0

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?

Alan A
  • 2,557
  • 6
  • 32
  • 54
  • use `.prop("checked",true|false)` instead of removing and adding because it is a property anyway and not an attribute – guradio Jul 06 '18 at 00:22
  • @guradio is 'attr' an outdated implementation of jQuery? Thanks for the info regarding trye false rather than removal. – Alan A Jul 06 '18 at 07:53
  • Consider reading this post: https://stackoverflow.com/questions/5874652/prop-vs-attr – Naftali Jul 06 '18 at 14:20

1 Answers1

2

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);
});
Alan Friedman
  • 1,582
  • 9
  • 7