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.

3
  • if checkbox is checked, do this. Commented Feb 1, 2013 at 8:16
  • why is he using <code>this.checked</code> instead of <code>$(this).checked</code> ? Commented Feb 1, 2013 at 8: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. Commented Feb 1, 2013 at 8:32

5 Answers 5

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/

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

3 Comments

could you tell me why the in the link in the abve comment, the person has used this.checked instead of $(this).checked ?
You can replace the line $('input[type="checkbox"]').each(function(){ to $('input[type="checkbox"]').click(function() This is the fiddle for it: jsfiddle.net/GQ4FY/2
@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 forum.jquery.com/topic/… and stackoverflow.com/questions/3633270/…
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");
        }
    });
});

Comments

0

try this:

$(function(){

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

});

1 Comment

This will apply the css to all spans irrespective of whether they are checked or not
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.

1 Comment

That book changed my life.
0

write your script as

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

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.