The value attribute of checkboxes in HTML


 Use the Value Attribute With Radio buttons and Checkboxes:

  • When a form gets submitted the data is sent to the server and includes entries for the options selected.
  • Inputs of type radio and checkbox report their values from the value attribute.
  • For example:

<label for="indoor">
  <input id="indoor" value="indoor" type="radio" name="indoor-outdoor">Indoor
</label>
<label for="outdoor">
  <input id="outdoor" value="outdoor" type="radio" name="indoor-outdoor">Outdoor
</label>


  • Here you have two radio inputs.
  • When the user submits the form with the indoor option selected, the form data will include the line: indoor-outdoor = indoor.
  • This is from the name and value attribute of the "indoor" input.
  • If you omit the value attribute, the submitted form data uses the default value.
  • IN this scenario if the user clicked the "indoor" option and submitted the form, the resulting form data would be indoor-outdoor = on, which is not useful.
  • So the value attribute needs to be set to something to identify the option.










No comments:

Post a Comment