Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .live() and Document ready

Tags:

jquery

I'm trying to set a CSS style using .live(). The CSS function:

$(".data tr:odd").addClass("evenrows"); 

Is there a way for this to happen automatically on document ready but still happen for future elements? I've got an ajax event on the page that re-orders a table. I need these newly created rows to have the .addClass applied to them.

Thanks in advance.

like image 855
jay Avatar asked Nov 04 '09 16:11

jay


2 Answers

you could use the Livequery plugin. It binds itself to DOM mutation events to track changes in the DOM, and re binds and re executes code attached to them, e.g:

$(".data tr:odd").livequery(function(){
   $(this).addClass("evenrows");
});
like image 63
ekhaled Avatar answered Nov 09 '22 13:11

ekhaled


We've accomplished this by hooking into the Ajax event delegates and doing whatever we want there. See "Complete" here: http://docs.jquery.com/Ajax/jQuery.ajax#options

You would use Live to attach any event handlers to those new rows. See: http://docs.jquery.com/Events/live#typefn

like image 33
Mark Avatar answered Nov 09 '22 11:11

Mark