0

I'm facing some problems with some jQuery or HTML.

I have some checkboxes. And I want to have a "primary" checkbox so a few checkboxes under that category get checked when the "primary" gets checked.

Well, that's OK. I solved it like this:

    $('#extraPolish').change(function() {
    if ($("#extraPolish").is(':checked') == true) {
        $("#extraPolish_Inside").attr('checked', 'checked');
        $("#extraPolish_UnderFender").attr('checked', 'checked');
        $("#extraPolish_OverFender").attr('checked', 'checked');
        console.log('checkd');
    } else {
        $("#extraPolish_Inside").removeAttr('checked');
        $("#extraPolish_UnderFender").removeAttr('checked');
        $("#extraPolish_OverFender").removeAttr('checked');
    }
});

So when you check #extraPolish those under there get checked, and when you remove check on #extraPolish those will be unchecked.

The problem is when i try to check them again it shows in the HTML code, but won't get checked on my Google Chrome.

Any Idea?

9
  • could you link to a jsfiddle? Commented Feb 20, 2013 at 18:13
  • Only fails on Google Chrome? Please add a Google Chrome tag. Commented Feb 20, 2013 at 18:14
  • What if you use $('#checkboxid').checked = $('#extraPolish').checked ? Saves an if statement. Commented Feb 20, 2013 at 18:15
  • Works fine here jsfiddle.net/j08691/rc9wC Commented Feb 20, 2013 at 18:19
  • jsfiddle.net/xxLLu Commented Feb 20, 2013 at 18:26

4 Answers 4

2

As others have stated - with jquery 1.9 prop() is the function you want .attr() does something different, missed that in the first place...

This would be my solution.

$(document).ready(function () {
    $('#extraPolish').change(function() {
        $("#extraPolish_Inside").prop('checked', this.checked);
        $("#extraPolish_UnderFender").prop('checked', this.checked);
        $("#extraPolish_OverFender").prop('checked', this.checked);
        return false;
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try using $("#extraPolish_Inside').prop('checked', true); and $("#extraPolish_Inside').prop('checked', false); instead of $("#extraPolish_Inside").attr('checked', 'checked');

Edit: jsfiddle example - http://jsfiddle.net/rvtvr/1

Comments

0

In stead of totally removing the checked attribute, try setting it simply to null.

$('#extraPolish_Inside').attr('checked',null); 

Here is a simple demo

Comments

0

Use .prop('checked', false); and .prop('checked', true); instead.

$(document).ready(function () {
    $('#extraPolish').change(function() {
        if ($("#extraPolish").is(':checked') == true) {
            $("#extraPolish_Inside").prop('checked', true);
            $("#extraPolish_UnderFender").prop('checked', true);
            $("#extraPolish_OverFender").prop('checked', true);
            console.log('checkd');
        } else {
            $("#extraPolish_Inside").prop('checked', false);
            $("#extraPolish_UnderFender").prop('checked', false);
            $("#extraPolish_OverFender").prop('checked', false);
        }
        return false;
    });
});

jsFiddle example

See http://jquery.com/upgrade-guide/1.9/#attr-versus-prop- for a full explanation and why the behavior differs between jQuery 1.9 and earlier.

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.