Javascript DOM HTML Input Radio get all radio buttons

Introduction

Several radio buttons in a form:

View in separate window

<!DOCTYPE html>
<html>
<body>

<p>How would you like your coffee?</p>

<form action="/action_page.php">
  <input type="radio" name="coffee" value="cream">A<br>
  <input type="radio" name="coffee" value="sugar">B<br>
  <br>
  <input type="button" onclick="myFunction()" value="Send order">
  <br><br>
  <input type="text" id="order" size="50">
  <input type="submit" value="Submit">
</form>/* ww  w  . j a  v  a 2 s  .c om*/

<script>
function myFunction() {
  var coffee = document.forms[0];
  var txt = "";
  var i;
  for (i = 0; i < coffee.length; i++) {
    if (coffee[i].checked) {
      txt = txt + coffee[i].value + " ";
    }
  }
  document.getElementById("order").value = "You ordered a coffee with: " + txt;
}
</script>

</body>
</html>



PreviousNext

Related