Javascript Reference - HTML DOM Input Radio name Property








The name property sets or gets the name attribute of a radio button.

The name value identifies form data after it has been submitted to the server.

We can also use name value to reference form data using JavaScript on the client side.

Browser Support

name Yes Yes Yes Yes Yes

Syntax

Return the name property.

var v = radioObject.name 

Set the name property.

radioObject.name=name




Property Values

Value Description
name Set the name of the radio button

Return Value

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

Example

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


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

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the name of a checkbox.


<!DOCTYPE html>
<html>
<body>
<!--   www .  j  a  v a 2 s.  com-->
<form>
<input type="radio" name="colors" value="red" id="myRadio">Red<br>
</form>
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>

<script>
function display() {
    var x = document.getElementById("myRadio").name;
    console.log("The name is: " + x);
}

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

</body>
</html>

The code above is rendered as follows: