HTML DOM Input Radio value Property


The HTML DOM Input radio value property is associated with the input element having type=”radio” and the value attribute. It is used to return the value of radio button value attribute or to set it.

The value property for radio button doesn’t affect the user interface of the website as the content is simply not displayed. However it can be used to distinguish between several buttons of the same group when the form is submitted.

Syntax

Following is the syntax for −

Set the value property −

radioObject.value = text;

Here, text is used for specifying the value for the radio button.

Example

Let us look at an example for the input radio value property −

 Live Demo

<!DOCTYPE html>
<html>
<body>
<h1>Input radio Value property</h1>
<form>
FRUIT:
<input type="radio" name="fruits" id="BTN1" value="Mango" >Mango
<br>
</form>
<p>Get the above element value by clicking the below button</p>
<button type=”button” onclick="getValue()">Get Value</button>
<p id="Sample"></p>
<script>
   function getValue() {
      var t = document.getElementById("BTN1").value;
      document.getElementById("Sample").innerHTML = "The value for the radio button is : "+t;
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the “Get Value” button −

In the above example −

We have first created an input element inside a form with type=”radio”, name=”fruits”, id=”BTN1” and value “Mango” −

<form>
FRUIT:
<input type="radio" name="fruits" id=”BTN1" value=”mango>Mango
</form>

We then created a “GET Type” button that will execute the radioType() method on being clicked by the user −

<button type=”button” onclick="radioType()">GET Type</button>

The getValue() method gets the input element using the getElementById() method and assigns its “value” attribute value to variable t. It will return an empty sting if the value attribute has not been specified for the radio button.This variable is then displayed in the paragraph with id “Sample” using its innerHTML property −

function getValue() {
   var t = document.getElementById("PASS1").value;
   document.getElementById("Sample").innerHTML = "The value for the input field is : "+t;
}

Updated on: 22-Aug-2019

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements