scottrakes
scottrakes

Reputation: 735

IE jquery takes two clicks to work

I am trying to setup a checkbox that checks all the child checkboxes, like a tree view. The following codes works great in Firefox and Chrome but in IE, it takes two clicks to get it to work. What am I missing?

HTML

<input type="checkbox" name="org" value="orgs" id="orgs" />
<label for="All">Organizations</label>

<ul id="orglist">
    <li>
            <input type="checkbox" name="org" value="1" id="1" />
            <label for="1">Org 1</label>
    </li>
    <li>
            <input type="checkbox" name="org" value="1" id="2" />
            <label for="2">Org 2</label>
    </li>
</ul>

Javascript/Jquery

$(document).ready(function () {
    $("#orgs").change(function () {

        if ($(this).attr("checked")) {
            $("#navform #orglist :checkbox").attr("checked", true);
        }
        else {
            $("#navform #orglist :checkbox").attr("checked", false);
        }
    });
});

Upvotes: 0

Views: 692

Answers (3)

Matt
Matt

Reputation: 11

For me the problem was I was just using checked and not checked="checked". Dumb mistake, but hopefully that helps someone.

Upvotes: 1

a7drew
a7drew

Reputation: 7821

Instead of looking for an attribute, let jQuery do more work:

if ($(this).is(":checked")) {
    $("#navform #orglist :checkbox").attr("checked", "checked");
}
else {
    $("#navform #orglist :checkbox").removeAttr("checked");
}

Upvotes: 0

ChrisLively
ChrisLively

Reputation: 88074

Most likely the state of a checkbox is in doubt for IE. Try setting the checked property to false or whatever on initial page load. For example:

<input type="checkbox" name="or" value="1" id="1" checked="false" />

Upvotes: 0

Related Questions