Javascript Reference - HTML DOM Input Radio value Property








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

Browser Support

value Yes Yes Yes Yes Yes

Syntax

Return the value property.

var v = radioObject.value 

Set the value property.

radioObject.value=text

Property Values

Value Description
text Set the value associated with the input radio button




Return Value

A String type value representing the value attribute of the radio button.

Example

The following code shows how to change the value attribute of a radio button.


<!DOCTYPE html>
<html>
<body>
<input type="radio" name="colors" value="red" id="myRadio">old name
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>
<!--from  w  ww  .j a v a 2  s  . c  om-->
<script>
function display() {
    var x = document.getElementById("myRadio").value;
    console.log("The value is: " + x);
}

function change() {
    var x = document.getElementById("myRadio").value = "newRadioBtnValue";
    console.log("The value was changed to: " + x);
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to display the value of the selected radio button.


<!DOCTYPE html>
<html>
<body>
<p>Select your favorite browser.</p>
<form action="url">
  <input type="radio" name="browser" onclick="myFunction(this.value)" value="Internet Explorer">Internet Explorer<br>
  <input type="radio" name="browser" onclick="myFunction(this.value)" value="Firefox">Firefox<br>
  <input type="radio" name="browser" onclick="myFunction(this.value)" value="Opera">Opera<br>
  <input type="radio" name="browser" onclick="myFunction(this.value)" value="Google Chrome">Google Chrome<br>
  <input type="radio" name="browser" onclick="myFunction(this.value)" value="Safari">Safari<br><br>
<!--   w w  w  .  j  av  a 2 s.  com-->
  Your favorite browser is: <input type="text" id="result">
  <input type="submit" value="Submit form">
</form>

<script>
function myFunction(browser) {
    document.getElementById("result").value = browser;
}
</script>
</body>
</html>

The code above is rendered as follows:

Example 3

The following code shows how to get the value of a radio button.


<!DOCTYPE html>
<html>
<body>
<input type="radio" name="colors" value="red" id="myRadio">Red
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!-- ww  w .  j a  va2s. co  m-->
    var x = document.getElementById("myRadio").value;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: