Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

removeEventListener not working

I have not been able to get the removeEventListener to work and have included a simple example below.

Basically if you click button one, you get an alert event. Clicking button two should remove the click eventListener from button 1…. but its doesn't.

Anyone else ever had the same issue ?

Or am I doing something wrong here ?

var testButton = Titanium.UI.createButton({
    top:30,
    width:100,
    height:33,
    title:'button 1'
});
testButton.addEventListener('click', function() {
    alert('Hello, world!');
});

var testButton2 = Titanium.UI.createButton({
    top:70,
    width:100,
    height:33,
    title:'button 2'
});
testButton2.addEventListener('click', function() {
    testButton.removeEventListener('click');
});

win1.add(testButton);
win1.add(testButton2);

Thanks
Jamesy

— asked June 9th 2010 by Richard James
  • button
  • eventlistener
  • removeeventlistener
1 Comment
  • I see the only way to do it is by asigning it to a var
    but… can it be done without that?
    like this…

    var testButton = Titanium.UI.createButton({
        top:30,
        width:100,
        height:33,
        title:'button 1'
    });
    testButton.addEventListener('click', function() {
        alert('Hello, world!');
    });
    
    var testButton2 = Titanium.UI.createButton({
        top:70,
        width:100,
        height:33,
        title:'button 2'
    });
    testButton2.addEventListener('click', function() {
        testButton.removeEventListener('click',function(){
            alert('Hello, world!');
        });
    });
    
    win1.add(testButton);
    win1.add(testButton2);
    
    — commented July 24th 2012 by Eugenio Melendez

5 Answers

  • Accepted Answer

    function SayHello() {
        alert('Hello, world!');
    }
    
    var click_b1 = testButton.addEventListener('click', SayHello);
    
    testButton2.addEventListener('click', function() {
        testButton.removeEventListener('click', SayHello);
    });
    
    — answered June 9th 2010 by Alan McConnell
    permalink
    5 Comments
    • this is the right way to do it. I had the same issue a while ago ;)

      — commented June 9th 2010 by Kosso
    • Yes I tried it and it worked… bit disappointing it has to be done this way though. Thanks Alan for your answer.

      — commented June 10th 2010 by Richard James
    • You have to add an event listener to remove a previous one? If so, you'll never be able to remove all the listeners. As soon as you create one, you'll need to create another to remove the first. That's just weird. Tell me it ain't so!

      — commented August 17th 2011 by Michael Stelly
    • Of course not. The example shown just created another button so that you could test the process within an app. You can write anywhere "testButton.removeEventLister(…)" you don't need to attach it to a separate event.

      — commented August 19th 2011 by Joshua Nicholson
    • Thanks!!! It worked well for me :)

      — commented January 4th 2012 by sathya nallusamy
  • Same here - this seems to work IF there are no arguments passed into the function but it does not work if there are argumetns. WTF

    — answered July 13th 2012 by Amy H
    permalink
    0 Comments
  • try this way

    var click_b1 = testButton.addEventListener('click', function() {
        alert('Hello, world!');
    });
    
    testButton2.addEventListener('click', function() {
        testButton.removeEventListener('click', click_b1);
    });
    
    — answered June 9th 2010 by Dan Tamas
    permalink
    1 Comment
    • No that does not work either… it's really annoying the removeEventListener (and among many other things I have found) does not do what it's supposed to.

      — commented June 9th 2010 by Richard James
  • No that does not work either… it's really annoying the removeEventListener (and among many other things I have found) does not do what it's supposed to.

    Does anyone know if this is known bug ?

    — answered June 9th 2010 by Richard James
    permalink
    0 Comments
  • Try this:

    textArea.addEventListener('postlayout', eventListener = function(e) { textArea.removeEventListener('postlayout', eventListener); var height = e.source.rect.height; textArea.top = that.imageData.height - height - 5; });

    — answered March 12th 2013 by Joel Koett
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.