SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Feb 2004
    Location
    Holland
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Changing/removing event listener

    Code:
    function pet() {
    	// General stuff.
    }
    
    pet.prototype.speak = function(e) {
    	alert("mmm");
    };
    
    
    function dog() {
    	// Doggy related things...
    }
    
    dog.prototype = new pet();
    
    dog.prototype.speak = function(e) {
    	alert("WOOF!")
    }
    
    var aDog = new dog();
    
    anElement.addEventListener("mousedown", function(e) { aDog.speak(e); }, false);
    Say I want to anElement to listen for cat.speak() instead of dog.speak(), how can I achieve this? (Mozilla only)
    Last edited by wira; Feb 18, 2007 at 08:25.

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Say I want to anElement to listen for cat.speak() instead of dog.speak(),
    The listener isn't listening for the function- it is listening for the event (mousedown) on anEement. You can change handlers for the event listened for by removing the defined handler and adding another:

    anElement.removeEventListener("mousedown", function(e) { aDog.speak(e); }, false);
    anElement.addEventListener("mousedown", function(e) { aCat.speak(e); }, false);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •