crak
crak

Reputation: 48

Radiobuttons set attribute checked is not working in function

Why is this not working with JQuery? The property wont be checked to true so or with asnyc function... I tried everything, but it only will working if i set the attribute checked true in browser at console. Not even with outsourced async callback functions.

for (var i = 0; i < user.benutzer.length; i++)
                {
                    var user_var = user.benutzer[i].benutzer;
                    user_rep = user_var.replace(/ /g, '_');
                    $('#div_buttongroup').append('<label for="' + user_rep + '" class="btn btn-primary" id="label_'+user_rep+'"><input type="radio" class="user_radio" id="' + user_rep + '" value="' + user_var + '" name="radio" autocomplete="off"></input>' + user_var + '</label>');
                    if (i === 0)
                    {
                        t = user_rep;
                       $('#label_'+user_rep).addClass("btn btn-primary active");
                        $('#'+user_rep).prop("checked",true);
                    }       
            }

Upvotes: 0

Views: 31

Answers (2)

crak
crak

Reputation: 48

I think it was a JQuery bug in combination with radiobuttons. I used a selectbox and everything was working

Upvotes: 0

Salomon Zhang
Salomon Zhang

Reputation: 1565

The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.

$('#'+user_rep).attr("checked");

Upvotes: 1

Related Questions