Javascript DOM HTML Input Checkbox value Property set

Introduction

Change the value associated with the checkbox:

document.getElementById("myCheck").value = "newCheckboxValue";

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

View in separate window

<!DOCTYPE html>
<html>
<body>
A Checkbox: <input type="checkbox" id="myCheck" value="myvalue">
<p id="demo"></p>
<button onclick="display()">Display name</button>
<button onclick="change()">Change name</button>

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

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

</body>
</html>

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

For checkboxes, the contents of the value property do not appear in the web page.

If a checkbox is in checked state when the form is submitted, the name of the checkbox is sent along with the value property.

If the checkbox is not checked, no information is sent.




PreviousNext

Related