views:

32

answers:

1

For normal html-tables I use

 $('table.alt tr:even').addClass('bg');

for striping the table. But obviously that doesn't work with ajax. the index of every new table-row is "-1" and therefore every new row gets the class.

Even the Live Query-Plugin from http://brandonaaron.net can't help me. Any suggestions?

Simon

+1  A: 

Well, you can try adding line that code, plus one that removes all existing strips, to the Global .ajaxSuccess() event so that it runs every time an ajax call is complete, but that isn't a terribly efficient method if you're working with many different ajax calls with only a few that actually changes the table.

You can also define a function that you can include with the success callback of your ajax calls, such as

function stripTable(){
     $('table.alt tr').removeClass('bg').filter(':even').addClass('bg');
}

This will prevent strips from being messed up if you insert in new rows into existing tables.

Using CSS is of course the best way to do this, but since you're using jQuery for this I suspect that backward compatibility is a key issue here.

Yi Jiang
+1 I was about to answer the same. Well, here's a [demo](http://jsfiddle.net/7Ph2H/).
Reigel