0

I have the following HTML table. Each row contains a checkbox followed by some text.

<table>
    <tr>
        <td><input type="checkbox" /></td>
        <td>Name</td>
    </tr>
    <tr>
        <td><input type="checkbox" /></td>
        <td>Name2</td>
    </tr>
</table>

I need some help writing a script that will highlight (adding a class) the rows that have been checked and unchecking will remove that class.

6 Answers 6

2

http://codebins.com/codes/home/4ldqpb8

    $(":checkbox").change(function() {
        $(this).closest("tr").toggleClass("highlight", this.checked)
    })
Sign up to request clarification or add additional context in comments.

2 Comments

Out of interest why use closest instead of parents?
Because CLosest finds the first one and then stop searching for others while parents goes till top of the document and if you have nested trs it will process toggleClass on all of them. so closest is faster and efficient & less chances of error.
2
$("#yourTableId input:checkbox").change(function() {
        var $this = $(this);
        if ($this.is(":checked")) {
            $this.closest("tr").addClass("highlight");
        } else {
            $this.closest("tr").removeClass("highlight");
        }
    });

Comments

0
$('[type="checkbox"]').change(function(){
  $(this).parents('tr').toggleClass('highlight')
})

3 Comments

Hiya bruv, small note :) make it is(':checked') since it should be highlighted when checked.
Or you can just add the class in the html for the checkbox that are checked by default. Unless he has dynamic elements, I would do that and keep the js smaller.
you can not use parents as if the the table is inside anther table it will highlight all row in the hierarchy. use closest instead. also cant just toggle without checking if its checked or not.
0

Demo http://jsfiddle.net/328QV/

code

$(document).ready(function() {
    $("#Table input").click(function() {
        if ($(this).is(":checked")) {
            $(this).parent().parent().addClass("highlight");
        } else {
            $(this).parent().parent().removeClass("highlight");
        }
    });
});​

Comments

0

Check out this fiddle

JS

$(document).ready(function () {
    $("input:checkbox").change(function () {
        alert();
        if ($(this).prop("checked") == true) {
            $(this).closest('tr').addClass("checked");
        } else $(this).closest('tr').removeClass("checked");
    });
});

CSS

.checked {
    background-color:red;
}

Comments

0

Worked for me

      $(document).on('click','tr',function(){
    if ($(this).css('background-color')=="rgb(143, 162, 225)") {

        $(this).find('input[type="checkbox"]').prop('checked',false);

        $(this).css('background','#ffffff'); 
    } else{
        $(this).find('input[type="checkbox"]').prop('checked',true);

        $(this).css('background','#8FA2E1'); `enter code here`
    }
});

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.