I am doing like this :
$(".classname").bind("keypress",function(){
alert("event happened");
})
code similar to above, working only once,i mean, the first time u type and click enter , its working , but next time, its not at all reacting .
$("#id").bind("keypress",function(){
alert("haiii");
})
the second code working all the time, but the first code working only once . Also if second code is run once, the first code is not even running once . what is the solution ? i think i am missing some rules here, can u tell them so that i will search about them .
Thanks
The event binder should be always available; if it's not it's because you're changing the HTML structure (either appending or deleting nodes). In your case, you're dynamically changing HTML at runtime, you need to use
.on()
Try this instead of
.bind()
:Regarding your coding style, you should separate the event handlers and the functions that handle the events. For instance, instead of this where the handlers also execute code:
You should have something like this:
As frenchie notes, it's because your html structure has changed. This has been treated right by .live(), but now .on() is the successor. But you should use on() not on the element, but on the document: