Most of the similar questions that arose as I wrote this one were like this (where the user wishes to toggle an element's class using pure JS) or this (where the user wishes wishes to toggle other attributes using jQuery)
My question is a mixture of the two. I am aware that the el.classList.toggle()
method exists for toggling an element's class using pure JS, but I wish to toggle different attributes using pure JS, specifically the checked
attribute of some radio buttons, since this (annoyingly) does not change when different options are selected.
Is it possible to toggle the checked
attribute on my two radio buttons with pure JS (by toggling I mean remove the attribute from the element altogether if it is present, and add it if it is not)?
radios = document.getElementsByName("Floor");
for (i = 0; i < radios.length; i++) {
radios[i].addEventListener('change', function () {
this.(/*checked attribute*/).toggle()
}, false);
}
<input checked id="Floor" name="Floor" type="radio" value="0">Ground Floor
<input id="Floor" name="Floor" type="radio" value="1">First Floor
Edit: The possible duplicate question mentioned in the comments does not quite solve my problem. I don't need to change which radio button appears checked, I just want to toggle the checked
attribute, to make it easier for me to reference the button that is checked later on in the code. Of course, if there is another, easier way of doing this, I'm all ears.
` `@Html.RadioButton("Floor", 1, true) First Floor ` Which in Chrome's element inspector results in: `` `Ground Floor ` `` `First Floor ` – murchu27 Jul 26 '17 at 15:17