3

I have a table with multiple rows. Each row has a check button. How do i highlight the row (apply style I suppose) when the checkbox is selected?

1
  • Please try to give it a shot first and then come back with some code. Commented Apr 1, 2011 at 17:10

3 Answers 3

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

See toggleClass() and closest()

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

3 Comments

Looks like they both add the class. Typo?
@Kerin - it was. I revised my answer, made it more concise using .toggleClass.
Right on. Incidentally, you just taught me about .closest() - golly, me and jQuery are getting married someday.
0
$(":checkbox:checked").each( 
function() 
{ 
    if (this.checked) 
    {
        this.addclass("yourClass");
    }
}

Comments

0
$(function(){
    $(":checkbox").click(function(){

       var thischeck = $(this);
       var approved = (thischeck.is(':checked')) ? '1' : '0';
       if(approved) {
           //apply what ever you want
       }
    });
})(jQuery);

I would try this, not sure if it is an elegant solution.

Comments

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.