jay
jay

Reputation: 10325

jQuery .live() and Document ready

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.

Upvotes: 5

Views: 6791

Answers (3)

Brett Weber
Brett Weber

Reputation: 1907

Right now our JavaScript is setting up a Global ajax call function that accepts the parameters needed. Using a template function with extension parameters available to cause the changes you want to make could eliminate any unnecessary duplicate code and open possibilities for more manageable code.

Example:

GlobalObject.AjaxFunction(sUrl, SData, fnOnSuccess, fnOnError, oExtension)
{
   //Set up Ajax process

   // check type of extension as a function
   if (typeof oExtension === 'function')
   {
      oExtension();
   }
}

This would enable your code to have generic availability for after ajax processing.

Hope this helps in context with your needs, if I didn't quite understand comment to let me know!

Upvotes: 0

ekhaled
ekhaled

Reputation: 2930

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");
});

Upvotes: 5

Mark
Mark

Reputation: 10206

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

Upvotes: 1

Related Questions