Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alternate row colors using jquery

Tags:

jquery

row

Using jQuery and not CSS, is it possible to alternate row colors between records? If so can anyone provide a short code script on how to accomplish this?

like image 837
Oscar C Avatar asked Nov 14 '11 21:11

Oscar C


People also ask

How to change table row color in jQuery?

mytr"). click(function(){ $(this). css("background-color", "#000000"); }); }); And that will apply the event handler to all rows with the class mytr.

How do I change the selected HTML table row background color using jquery?

Change the 'background-color' property of the row in the click event handler of the row. Code would be: //attach an onclick event handler to the table rows $(“#myTable tbody tr”). click(function(){ //change the 'background-color' property to the desired color.


2 Answers

You can select tr elements from a table and css accepts a function as a parameter, which will return a value based on some criteria you decide. In this case, you can test the index for evenness.

$("#table tr").css("background-color", function(index) {
    return index%2==0?"blue":"red";
});

JSFiddle: http://jsfiddle.net/n3Zny/

like image 140
Dennis Avatar answered Oct 05 '22 07:10

Dennis


Try this:

$("tr:even").css("background-color", "#eeeeee");
$("tr:odd").css("background-color", "#ffffff");
like image 24
aknatn Avatar answered Oct 05 '22 07:10

aknatn