Pre-select Input Element on Page Load in HTML



Use the checked attribute to specify an <input> element is to be pre-selected on page load. The attribute works with −

<input type = "checkbox">
<input type = "radio">.

Example

You can try to run the following code to implement checked attribute in HTML −

 Live Demo

<!DOCTYPE html>
<html>
   <body>
      <p>Which sports do you like?</p>
      <form action = "" method = "get">
         <input type = "checkbox" name = "vehicle" value = "football" checked> Football<br>
         <input type = "checkbox" name = "vehicle" value = "cricket" checked> Cricket<br>
         <input type = "checkbox" name = "vehicle" value = "hockey"> Hockey<br>
         <input type = "submit" value = "Submit">
      </form>
   </body>
</html>
Updated on: 2020-05-28T00:22:39+05:30

158 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements