HTML DOM Input Checkbox value Property

HTMLWeb DevelopmentFront End Technology

The Input Checkbox value property returns a string with the value attribute of input checkbox. User can also set it to a new string.

Syntax

Following is the syntax −

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

Example

Let us see an example of Input Checkbox value property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Value Attribute of Checkbox</title>
</head>
<body>
<form id="Form">
<div>
Color-Red: <input value="Green" id="formCheckbox" type="checkbox" name="formCheckbox">
</div>
</form>
<button onclick="changeType()">Change value of input checkbox</button>
<div id="displayDiv"></div>
<script>
   var valueOfInput = document.getElementById("formCheckbox");
   var displayDiv = document.getElementById("displayDiv");
   displayDiv.textContent = 'Value: ' + valueOfInput.value;
   function changeType(){
      if(valueOfInput.value == 'Green' && valueOfInput.checked == true){
         valueOfInput.value = 'Red' displayDiv.textContent = 'value: ' + valueOfInput.value;
      } else {
         displayDiv.textContent = 'Check the checkbox to change value to red';
      }
   }
</script>
</body>
</html>

Output

This will produce the following output −

Before clicking ‘Change value of input checkbox’ button −

After clicking ‘Change value of input checkbox’ button −

Checked ‘Color-Red’ checkbox & clicking ‘Change value of input checkbox’ button −

raja
Published on 19-Jul-2019 12:00:41
Advertisements