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?
mytr"). click(function(){ $(this). css("background-color", "#000000"); }); }); And that will apply the event handler to all rows with the class mytr.
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.
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/
Try this:
$("tr:even").css("background-color", "#eeeeee");
$("tr:odd").css("background-color", "#ffffff");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With