views:

88

answers:

2

i have two tables. <table id='1'></table> and <table id='2'></table>. When i put this code:

$(document).ready(function()
{
  //for table row
  $("tr:even").css("background-color", "#F4F4F8");
  $("tr:odd").css("background-color", "#EFF1F1");

});

Both the tables got it alternate row colors, which i dont want, i want only to color the table with id=2. how it can be accomplished?

+2  A: 

Modify your code like:

$(document).ready(function()
{
  $("table#id2 tr:even").css("background-color", "#F4F4F8");
  $("table#id2 tr:odd").css("background-color", "#EFF1F1");
});

This assumes you have table with id set to id2 eg:

<table id="id2">
Sarfraz
using IDs like that is not allowed in HTML, and IE doesn't recognise it.
nickf
the use of an element selector with an ID selector is extra work on the selector engine (and browser for css) - elements should be specified for classnames only, such as '#id1 tr.myCoolEvenClass'. (in the above example #id1 already points to the table, so no need to say 'look at all the tables and find #id1)
Dan Heberden
@nickf: Yup I knew that but i was just extending his example although i should have suggested not use pure number as id. Thanks
Sarfraz
+6  A: 

First thing is that it's not allowed to have an id starting with a number. Change the tables to have ids something like this:

<table id="table1"></table>

Then, all you need to do is add the right selector into your jQuery:

$("#table2 tr:even").css(...);
$("#table2 tr:odd").css(...);
nickf