0

I'm going to put on my website simple radiobutton. His role is only to have value 'on' if checked or 'off' if not checked.

home.html

<input type="radio" id="radio1" class='radio-button' value="off">
<label >Radio1</label>
<br>
<input type="radio" id="radio2" class='radio-button' value="off">
<label >Radio2</label>

I wrote a loop in my js file code to iterate through the inputs and my goal is to change value to 'on' if checked.

myJs.js

const radioButtons = document.getElementsByClassName('radio-button')
for (let radio of radioButtons){if (radio.checked){ radio.value='on'}

But it doesn't work. Value stays 'off' even if radiobutoon is checked. Any idea what am I doing wrong?

user13137381
  • 95
  • 2
  • 9
  • 1
    radio butoon doesn't work this way.... – Mister Jojo Dec 10 '21 at 17:55
  • It looks like you're running the code at page load, at that time the buttons are unchecked. You've to add your code to an event listener, ex. `change` would work. Also, it looks like you'd need checkboxes instead of radio buttons. – Teemu Dec 10 '21 at 17:57
  • I tested your code and it should work, but you'll need to close your for loop with a } – E-telier Dec 10 '21 at 18:35

0 Answers0