HTML DOM Input Checkbox name Property


The HTML DOM Input Checkbox name property returns a string, which is the value of the name attribute of input checkbox. User can also set it to a new string.

Syntax

Following is the syntax −

  • Returning string value
inputCheckboxObject.name
  • Setting name attribute to a string value
inputCheckboxObject.name = ‘String’

Example

Let us see an example of Input Checkbox name property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Name Attribute of Checkbox</title>
</head>
<body>
<form id="Form">
<div>
Enable new name: <input id="formID" type="checkbox" name="formID">
</div>
</form>
<button onclick="getFormID()">Change Name Attribute</button>
<div id="nameAttr"></div>
<script>
   function getFormID(){
      var oldNameAttr = document.getElementById("formID");
      var newNameAttr = document.getElementById("nameAttr");
      if(oldNameAttr.checked == true){
         oldNameAttr.name = 'newFormID';
         newNameAttr.textContent = 'Updated value of name attribute: '+oldNameAttr.name;
      } else {
         newNameAttr.textContent = 'Check the checkbox'+ ', current value of name attribute: '+oldNameAttr.name ;
      }
   }
</script>
</body>
</html>

Output

This will produce the following output −

Before checking ‘Enable new name’ checkbox −

After checking ‘Enable new name’ checkbox −

Updated on: 30-Jul-2019

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements