Search code examples
jqueryattributescheckedprop

Jquery removeAttr and add Attribute


I am trying to do something very simple. I have 10 input fields and one of these has the checked attribute. I want to remove this attribute and apply it to another known input field.

My code is as follows:

$(document).ready(function() {
    $('input.article-vote').removeAttr('checked');
    $('input#rating' + id).attr('checked', 'checked');
});

As can be seen I am remove the attribute by class and selecting the target by id.

When I run this code the checked attribute is removed but the target isn't applied. If I remove the removeAttr line then the target has the attribute applied.

How can I remove all and apply to a target of my choice?


Solution

  • You don't want to remove the checked attribute, but rather set it to true or false.

    $(document).ready(function() {
      $('input.article-vote').prop('checked', false);
      $('input#rating' + id).prop('checked', true);
    });