loquaz Posted May 12, 2006 Share Posted May 12, 2006 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 More sharing options...
biofreakz Posted May 13, 2006 Share Posted May 13, 2006 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 More sharing options...
loquaz Posted May 15, 2006 Author Share Posted May 15, 2006 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 More sharing options...
aspnetguy Posted May 15, 2006 Share Posted May 15, 2006 you could have written it like this xAddEventListener(_children[i], "click", function(){t("works fine!")}, false) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now