Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery live event gets executed twice

Tags:

jquery

I am loading content into a page with ajax that will have live click events attached to them. It seems like the first time I load this content everything works as expected. If I load this again, the events fire twice (when put breakpoints in the code). What am I doing wrong? Is there a way I can clear the dom of these elements before they get loaded again?

like image 946
Victor Avatar asked Apr 13 '10 09:04

Victor


3 Answers

I guess you're calling .live() on the same elements again and again. use .die() on those elements before calling .live().

like image 120
jAndy Avatar answered Nov 18 '22 05:11

jAndy


I fixed it like this:

$('selector').die('click').live('click', function () {
  // Do stuff here
});
like image 25
Marian Zburlea Avatar answered Nov 18 '22 06:11

Marian Zburlea


just confirmed. Was using .live() and experimented with .unbind() first.

final:

$(".clickedClass").die("click").live("click", function () {

});
like image 2
seth yount Avatar answered Nov 18 '22 06:11

seth yount