views:

253

answers:

2

Okay, I want some jquery/ js to show an yellow background-color when its checked... how to do ? (I use class="valgt" to define which checkbox i want to be styled.

 <td valign="top" class="valgt">    
<input  type="checkbox" name="check" value="" /> 
 </td>

I have no ideá how to fix it.. so I need some code help for the jquer/ js script

+1  A: 

Declare your bg color in the css class

.valgt { background-color : #ffff00; }

then use the following to add or remove this class from the parent td dependent on the checked state

$('input[name=check]').click( function(){

   var $el = $(this);
   $el.is(':checked') ? $el.parent().addClass('valgt') 
                      : $el.parent().removeClass('valgt');

})
redsquare
+2  A: 

Try this. It would be easier if you put an ID or name on your td.

$('input[name=check]').click(function() {
    var chkbox = $(this);
    if (chkbox.is(':checked'))
        chkbox.parent().addClass('valgt');
    else
        $chkbox.parent().removeClass('valgt');
});
John Oxley
wow! thx alot, that worked just fine. no prob at all! I appreciate your help! :)
william
as a general rule beware of excessive creation of jQuery objects e.g $(this). Do it once and cache it.
redsquare
@redsquare - alright, thx alot. I will remember that!
william
Thanks @redsquare. Fixed
John Oxley