Home > Software engineering >  Add css class to tr if it is even
Add css class to tr if it is even

Time:01-06

I am adding dynamic row in table using below code.

Now I want to check if this added row is odd OR even, if it is even then apply some css class to tr otherwise do not apply that css class.

Any idea how I can get this working.

$('#templateListData > tbody:last-child').append('<tr id=row_'   response.id   '> <td scope=row style=min-width: 60px;word-break:break-all;>'   response.name   '</td> <td scope=row style=word-break:break-all;min-width: 50px;>'   response.code   '</td> <td> <a  target=_blank href=/Template/Edit?id='   response.id   '> Edit </a> <button  onclick=showDeletePopUp('   response.id   ',Template); return false;>Remove</button> </td> </tr> ');

CodePudding user response:

You can do in this way

$("#tabaleid tbody tr:even").css("background-color", "yellow");

Here yellow is just for reference.

Let me know if that works or not, if not then I can give some other suggestion.

Thanks

CodePudding user response:

I think jquery already has a solution for this written below:

$("#TableID tbody tr:even").addClass("even");

Using this all the even tr of your table will have this class added.

  •  Tags:  
  • Related