0

I have tried to highlight the row in a table using jQuery when the checkbox is checked. Used this jQuery code for the same.

$(":checkbox").change(function() {
    $(this).closest("tr").toggleClass("highlight", this.checked);
});

I'm getting it correct for only alternate rows. I have used the following CSS to style the alternate rows in the table.

.tables tbody tr:nth-child(2n){
    background-color:#F9F9F9;
}

How can I highlight the even rows too?

5
  • Could you include the <table></table> markup here? Commented Sep 16, 2012 at 18:44
  • You can check that in the link above! Commented Sep 16, 2012 at 18:45
  • Right, but if your page goes down then this question won't be as useful anymore :-/ Commented Sep 16, 2012 at 18:46
  • But the table code is very large. It includes a lot of other markups. Commented Sep 16, 2012 at 18:47
  • anddd 3 years later, the page is down Commented Apr 27, 2015 at 8:40

1 Answer 1

1

Just add this style to your css file

.tables tr.highlight td {
  background-color: #FFF9DC;
}

The problem is that your even rows styles overrides highlight style. So all you should do is to make your highlight style more important.

Sign up to request clarification or add additional context in comments.

3 Comments

One more question. I have another checkbox in the same row. When I click in that checkbox also, the row is highlighting (which is not needed). How do I trigger the jQuery only for the first checkbox?
Add some class to checkboxes you want to behave in this way. And bind jQuery with something like this $(".some-class:checkbox").change(function()...
May be try like this $(".A input:checkbox").change(function() Where A is a class for the checkbox!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.