0

There are two lines from my js code:

document.getElementsByName('group')[0].removeAttribute('disabled');
document.getElementsByName('group')[0].removeAttribute('checked');

First string works good but second one makes nothing. I want to follow html5 style so setting false value for checked isn't my choice. And by the way I look for method to set empty attrs. Methods from other topics like .setAttribute(attr_name,""); do exactly what they are: create attr with empty string value but not just attr. What should I do?

Roman
  • 439
  • 1
  • 9
  • 15

2 Answers2

1

checked is a property, you should treat it as such:

document.getElementsByName('group')[0].checked = false;
andlrc
  • 41,466
  • 13
  • 82
  • 108
  • I agree. Attributes have values and as far as I've seen `key=value` type assignation. Properties are either there or not. You don't need `checked=true` or `checked=checked` etc. People do it and it works I believe because browsers and libraries know that no one understands the actual spec so they help you out. Regardless of it jquery or browsers know fill in the gaps, you should treat it as a property and not a attribute. http://stackoverflow.com/questions/6003819/properties-and-attributes-in-html – Leeish May 27 '16 at 21:56
  • Thanks! Can you clarify that for me? "Checked" is property, but "disabled" - attribute. Right? It confuses me... – Roman May 27 '16 at 22:26
  • @Antipers007 Leeish posted a good link that will explain why and what in more details :-) https://stackoverflow.com/questions/6003819/properties-and-attributes-in-html – andlrc May 27 '16 at 23:12
  • @andlrc, sorry, I didn't notice it. Thank you – Roman May 27 '16 at 23:21
0

I show that your code works. here is the jsfiddle. Show us your checkbox group.
You will see that when run, the first, aka [0] disabled is removed while the input with value='2' still is disabled.

https://jsfiddle.net/Esko/m7o0z0gc/

<input name="group" type="checkbox" checked="checked" disabled='disabled' value='1' /> <input name="group" type="checkbox" checked="checked" disabled='disabled' value='2' /> <input name="group" type="checkbox" checked="checked" value='3' />

Esko
  • 669
  • 10
  • 21