Javascript DOM Document get element by name

Description

Javascript DOM Document get element by name

View in separate window


<html>
<head>
<title>getElementsByName() Example</title>
</head>/*from   ww w .j  a v a2  s.c o  m*/
<body>
<form>
  <p>
    What is your favorite color?: <Br />
    <input type="radio" name="favColor" value="red"> Red<br />
    <input type="radio" name="favColor" value="blue"> Blue<br />
    <input type="radio" name="favColor" value="green"> Green<br />
    <input type="radio" name="favColor" value="orange"> Orange<br />        
  </p>
</form>
  <script>
    
    let favColorGroup = document.getElementsByName("favColor");
    for (let cI = 0; cI < favColorGroup.length; cI++)
      console.log("Color: " + favColorGroup[cI].value);

  </script>
</body>
</html>



PreviousNext

Related