The checked attribute is used to preselect a checkbox or radio button when a webpage loads. It helps in setting default choices in forms.
- Automatically selects a checkbox or radio button by default.
- Mainly used with <input type="checkbox"> and <input type="radio">.
- It is a boolean attribute, so its presence alone makes the input checked.
Syntax:
<input type = "checkbox|radio" checked> Example: The first checkbox is checked by default using checked, while the second checkbox is unchecked by default.
<!--Driver Code Starts-->
<!DOCTYPE html>
<html>
<head>
<title>
HTML <input>checked
Attribute
</title>
</head>
<body style="text-align: center;">
<h1 style="color: green;">
GeeksforGeeks
</h1>
<h2>
HTML <input> checked Attribute
</h2>
<!--Driver Code Ends-->
<form>
<!-- Below input elements
have attribute "checked" -->
<input type="checkbox"
name="check"
value="1" checked>
Checked by default
<br>
<input type="checkbox"
name="check" value="2">
Not checked by default
<br>
</form>
<!--Driver Code Starts-->
</body>
</html>
<!--Driver Code Ends-->