Javascript Reference - HTML DOM Input Checkbox name Property








The name attribute from input checkbox element identifies form data after submission.

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

The name property sets or gets the value of the name attribute of a checkbox.

Browser Support

name Yes Yes Yes Yes Yes

Syntax

Return the name property.

var v = checkboxObject.name 

Set the name property.

checkboxObject.name=name




Property Values

Value Description
name Set the name of the checkbox

Return Value

A String type value representing the name of the checkbox.

Example

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


<!DOCTYPE html>
<html>
<body>
<!--  w ww  . j a  va 2  s .c o m-->
A Checkbox: <input type="checkbox" id="myCheck" name="myname">
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>

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

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

</body>
</html>

The code above is rendered as follows: