The following error occurred while working with radiobutton: document.getElementByName is not function. How to fix it? Below is a line of code in which this error is observed. Full code below.

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <p> <input type="radio" name="prim" value="груши">Яблоки</p> <p> <input type="radio" name="prim" value="груши">Груши</p> </body> </html> <script> var radio = document.getElementByName('prim'); for (var i=0; i<radio.length; i++) { radio[i].onchange = testRadio; } function testRadio() { console.log (this.value); } </script> 
  • I edited the question, added the full code. - Vasya Vasinn
  • Yes, there really is no such function and never in browsers - andreymal
  • There is getElementsByName , there is no getElementByName . - Suvitruf
  • Possible duplicate question: How to make a check radiobutton in javascript? - greg zakharov

1 answer 1

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <p> <input type="radio" name="prim" value="Груши">Яблоки</p> <p> <input type="radio" name="prim" value="Груши">Груши</p> </body> </html> <script> var radio = document.getElementsByName("prim"); for (let i=0; i<radio.length; i++) { radio[i].onchange = testRadio; } function testRadio() { console.log (this.value); } </script> 

It was necessary to getElementsByName, not getElementName

  • Thanks for the answer! - Vasya Vasinn
  • Just tick off that this is the answer, I will be grateful - user299729