1

HTML:

<td>
    <input type="checkbox" name="lit" value="literary"><span class="checkdata">&nbsp;Literary Event&nbsp;&nbsp;</span>
    <input type="checkbox" name="art" value="art"><span class="checkdata">&nbsp;Art Event&nbsp;&nbsp;</span>
    <input type="checkbox" name="cul" value="cultural"><span class="checkdata">&nbsp;Cultural Event&nbsp;&nbsp;</span>
</td>

Script:

<script>
    $(function(){
        if ("input:checked") {
            $(".checkdata").css("color", "#F0F0F0");
        }
    });
</script>

What I'm trying to do here is to check, if the user has checked the checkbox. If so, the span class="checkdata" should turn grey. It does not happen though.

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • [if checkbox is checked, do this](http://stackoverflow.com/questions/4243554/if-checkbox-is-checked-do-this). – Vucko Feb 01 '13 at 08:16
  • why is he using this.checked instead of $(this).checked ? – anubhav gupta Feb 01 '13 at 08:18
  • @anubhavgupta, in his post he links to a pretty great writeup. Bottom line is, there's no real need to encapsulate "this" into a jquery object, as it only creates overhead (=slower) and isn't needed at all. – Kippie Feb 01 '13 at 08:32

5 Answers5

1

Try This

$(function(){
    $('input[type="checkbox"]').each(function(){
        $(this).click(function(){
        if ($(this).prop('checked'))
        {
            $(this).next(".checkdata").css("color", "#F0F0F0");
        }
        else
            $(this).next(".checkdata").css("color", "#000000");

        });
    });
});

Test fiddle: http://jsfiddle.net/GQ4FY/1/

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • could you tell me why the in the link in the abve comment, the person has used this.checked instead of $(this).checked ? – anubhav gupta Feb 01 '13 at 08:25
  • You can replace the line `$('input[type="checkbox"]').each(function(){` to `$('input[type="checkbox"]').click(function()` This is the fiddle for it: http://jsfiddle.net/GQ4FY/2/ – Rohan Kumar Feb 01 '13 at 08:38
  • @anubhavgupta Why not Anubhav In short `this is the DOM object, whereas $(this) is the jQuery wrapper around same.` but for more info you can read from here http://forum.jquery.com/topic/difference-between-this-and-this-and-when-i-should-use-which and http://stackoverflow.com/questions/3633270/difference-between-this-and-this-in-jquery – Rohan Kumar Feb 01 '13 at 08:45
0

try this:

$(function(){

    if($("input").is(':checked')){
        $(".checkdata").css("color", "#F0F0F0");
    }

});
Jamir Khan
  • 397
  • 1
  • 13
0

If you want to toggle the css when the checkbox value is changed, try this:

$(function () {
    $("input[type='checkbox']").change(function () {
        var isChecked = $(this).is(":checked");
        if (isChecked) {
            $(".checkdata").css("color", "#F0F0F0");

        } else {
            $(".checkdata").css("color", "#000000");
        }
    });
});
Catalin
  • 11,503
  • 19
  • 74
  • 147
0
 $("input[type='checkbox']").change(function (){
var perform = {
   true : {'color' : 'green'},
   false: {'color' : 'red'}
 }, chk = $(this);

 $(".checkdata").css(perform[chk.is(":checked")]);

});  

Prevent if statements :) Polymorphism.

Philll_t
  • 4,267
  • 5
  • 45
  • 59
0

write your script as

$(function() {
    $('input[type="checkbox"]').click(function() {
        $(this).next('.checkdata').css('color', this.checked ? '#F0F0F0' : '#000000');
    });
});
pbaris
  • 4,525
  • 5
  • 37
  • 61