I have this jQuery code:
$("div.note a").live("click", function(e) {
e.preventDefault();
alert('test');
});
<div id="note_list">
<div class="note">
Text 1
<a href="">X</a>
</div>
<div class="note">
Text 2
<a href="">X</a>
</div>
<div class="note">
Text 3
<a href="">X</a>
</div>
</div>
Could someone tell me why the alert shows 3 times? It works fine in Chrome but not in Firefox.
I have this jQuery code:
$("div.note a").live("click", function(e) {
e.preventDefault();
alert('test');
});
<div id="note_list">
<div class="note">
Text 1
<a href="">X</a>
</div>
<div class="note">
Text 2
<a href="">X</a>
</div>
<div class="note">
Text 3
<a href="">X</a>
</div>
</div>
Could someone tell me why the alert shows 3 times? It works fine in Chrome but not in Firefox.
->
If there is a problem it is somewhere else in your code. Maybe you are adding the elements dynamically and binding the event handler every time? Then you did not understand what live
is doing: api.jquery./live
– Felix Kling
Commented
Oct 19, 2011 at 13:56
live
is good when it is used correctly. But as it stands, we cannot reproduce your problem which either means you don't have a problem or you provide more code which might be related to the problem or you have to solve it by yourself.
– Felix Kling
Commented
Oct 19, 2011 at 14:04
It is called onetime, in your case you can stop multiple event call by e.stopImmediatePropagation();
$("div.note a").live("click", function(e) {
e.stopImmediatePropagation();
e.preventDefault();
alert('test');
});
Try this
$("div.note a").die('click').live("click", function(e) {
e.preventDefault();
alert('test');
});