Jump to content

EventListener Doesn't works


loquaz

Recommended Posts

Hi all, First of all, sorry by my english. I'm brazilian and i don't speak english very well :)I'm trying to add an eventlistener to an anchor element that i created on the fly using DOM and some functions from the x library. When my page that contains this function are loaded, the function are triggered one time on this load event (which is an error, because i'm using the 'click' mouse event) and then doesn't work anymore. I'm using xAddEventListener function from x library but when i made without using x library happen the same.i'll paste some code to ilustrate the situation: [this.test = function(_id){ _icons = xGetElementById(_id); _children = _icons.childNodes; for(i = 0 ; i < _children.length; i++ ){ if(_children.nodeName.toLowerCase() == 'a'){ xAddEventListener(_children, "click", t("works fine!"), false); } } }]"t" is my callback function that display a dialog box with the message contained in the unique argument passed to the function.Somebody can help me to solve this problem?

Link to comment
Share on other sites

xAddEventListener(_children[i], "click", t("works fine!"), false)

You are right it is the xAddEventListener function where the problem is. The problem is this t("works fine!"). The line calls the function t expecting it to return a function name for the handler, for the event listener. This is what you should be doing:

function t(){   alert("works fine!")}xAddEventListener(_children[i], "click", t, false)

You shouldn't call a function when assigning it to an event listener (well you can, but it is more complex). All you do is put the name of the function you wish to be called when the event is fired. So you shouldn't put t() as if you were calling the function, you should just put t.Hope this helpsAdam

Link to comment
Share on other sites

Thanks man! Your tip solved my problem. So, now i'll rewrite my code in another way, refactoring my callback function or passing the function declaration as a parameter to the eventListener. Thanks brow! [:)]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...