0

I'm selecting a checkbox and adding style to its parent tag. It's working well. What I don't know is how to do the inverse way, when it is already checked and remove the style.

http://jsfiddle.net/FG7jV/

HTML

<table>
    <tbody>
        <tr>
            <td><input type="checkbox" name="" value="" /></td>
            <td><img src="http://lorempixel.com/60/60/" /></td>
                    <td>Boné NFL - San Diego Charges</td>
         </tr>
      </tbody>
 </table>

JQUERY

$("input:checkbox").click(function() {
$(this).parent().parent().css("background-color","yellow");
});
$("input:checked").click(function(){
alert();
});

im using alert, obviously, just to check if it's selecting when i click.

3 Answers 3

1

You may try this (Use change event):

$("input:checkbox").change(function() {
    if($(this).is(':checked')) {
        $(this).parent().parent().css("background-color","yellow");
    }
    else {
        $(this).parent().parent().css("background-color","");
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

You can use .prop() to check if the checkbox is checked onclick:

$("input:checkbox").click(function() {
    if(!$(this).prop("checked"))
        alert();
    else
        $(this).parent().parent().css("background-color","yellow");
});

JSFiddle

Comments

1

You can use this:

$("input:checkbox").click(function() {
var isChecked = $(this).attr('checked')?true:false;
if(isChecked){
$(this).parent().parent().css("background-color","yellow");
}
else
     $(this).parent().parent().css("background-color","");
});

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.