Javascript DOM HTML Input Radio value Property get

Introduction

Get the value attribute of a radio button:

var x = document.getElementById("myRadio").value;

Click the button to display the value attribute of the radio button.

View in separate window

<!DOCTYPE html>
<html>
<body>

<input type="radio" name="colors" value="red" id="myRadio">Red color

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {/*w ww  .j  av  a 2  s  .  c om*/
  var x = document.getElementById("myRadio").value;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The value property sets or gets the value attribute of the radio button.

The contents of the value property for radio buttons do not appear in the user interface.

The value property will be passed to the server when submitting a form.

If a radio button is in checked, the name of the radio button is sent with the value property to the server.

If the radio button is not checked, no information is sent.

We should define different values for radio buttons in the same group to identify which one was checked.

The value property accepts and returns a String type value.




PreviousNext

Related