Javascript Reference - HTML DOM Select name Property








The name property sets or gets the name attribute of a drop-down list.

The name attribute 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 = selectObject.name 

Set the name property.

selectObject.name=name




Property Values

Value Description
name Set the name of the drop-down list

Return Value

A String type value representing the name of the drop-down list.

Example

The following code shows how to change the name attribute of a drop-down list.


<!DOCTYPE html>
<html>
<body>
<!--  w ww  .j a  v a2s  . c om-->
<form>
<select name="selectName" id="mySelect">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
</select>
</form>
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>

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

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

</body>
</html>

The code above is rendered as follows: