Javascript DOM HTML Input Checkbox name Property set

Introduction

Change the name of a checkbox:

document.getElementById("myCheck").name = "newCheckboxName";

Click the buttons to display/change the name attribute of the checkbox.

View in separate window

<!DOCTYPE html>
<html>
<body>

A Checkbox: <input type="checkbox" id="myCheck" name="myname">
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>
<p id="demo"></p>

<script>
function display() {
  var x = document.getElementById("myCheck").name;
  document.getElementById("demo").innerHTML = "The name of the checkbox is: " + x;
}

function change() {//from   w ww.  j  a v  a  2  s .co m
  var x = document.getElementById("myCheck").name = "newCheckboxName";
  document.getElementById("demo").innerHTML = "The name was changed to: " + x;
}
</script>

</body>
</html>



PreviousNext

Related