Thursday, July 19, 2007

I just came across a problem that drove me crazy for ages. I have an ajax call that updates some content then has to reinitialise the javascript to attach events to the new elements on the page. I use $.getScript() and call the js file, so far so good. The problem I encountered was that everytime I clicked on a tab which called this function and then on a button that I was setting up it was firing multiple times, and incrementing everytime I clicked on a tab which called the $.getScript(). So I figured that somehow the click event was being assigned to my button everytime the script reinitialised. The solution? unbind the problematic event, then when $.getScript() is called the button is reinitialised with the single click event!

$("#mybutton").unbind()

Simple when you know how!

Jon 5:17 PM Permalink

Comments:

Check out the one() function. Lets you set an event which will be run only once.
http://docs.jquery.com/Events#one.28_type.2C_data.2C_fn_.29

Post a Comment