user3439585
user3439585

Reputation: 87

jQuery | dynamically check checkbox

please check at first Jsfiddle: http://tinyurl.com/q58dnpo this little script works exactly how I imagined. It allows you to check just one checkbox and it dynamically syncs the checkboxes. So far so good.

BUT after pasting it into a more complex script the sync-function just works exactly one time clicking A,B and C. check it out here: http://tinyurl.com/ngbj2yk

is there something wrong with the header? I am really clueless, because on Jsfiddle all works fine even the more complex code (probably I am just a noob ;))

Thanks!

Upvotes: 2

Views: 427

Answers (2)

martynas
martynas

Reputation: 12290

You should use .prop() instead of .attr()

The following jQuery code should work just fine:

$(document).ready(function(){
    $('#eins').click(function(){
        $("#gruppe1").prop("checked", true);
    });

$('#zwei').click(function(){
        $("#gruppe2").prop("checked", true);
    });

$('#drei').click(function(){
        $("#gruppe3").prop("checked", true);
    });
});

Resources:

Upvotes: 0

adeneo
adeneo

Reputation: 318222

Everywhere you're currently using

$(element).attr("checked","checked");

change it to

$(element).prop("checked", true); // or false

as element.checked is a property, and the prop() method is the proper method to use, attr() will only change the attribute and cause the issues you're experiencing.

Upvotes: 4

Related Questions