HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML checked Attribute
Photo Credit to CodeToFun
🙋 Introduction
The checked
attribute is a fundamental feature in HTML, primarily associated with input elements of type checkbox or radio.
This attribute allows developers to preselect checkboxes or radio buttons, providing an initial state for these input elements.
🎯 Purpose of checked
The primary purpose of the checked
attribute is to set the initial state of a checkbox or radio button.
When the checked
attribute is present, the associated checkbox or radio button is selected by default when the page loads.
💎 Values
The checked
attribute is a boolean attribute, meaning it doesn't require a value. It can be present or absent.
If the attribute is present, it signifies that the checkbox or radio button should be checked by default. If it's absent, the checkbox or radio button is not checked initially.
📄 Example
Let's explore a simple example of using the checked attribute with a checkbox:
<input type="checkbox" id="subscribe" name="subscribe" checked>
<label for="subscribe">Subscribe to newsletter</label>
🧠 How it Works
In this example, the checkbox has the checked
attribute present, indicating that it should be checked by default.
🔄 Dynamic Values with JavaScript
You can also dynamically set or remove the checked
attribute using JavaScript.
This is useful when you want to change the state of checkboxes or radio buttons based on user interactions or certain conditions. Here's a brief example:
<script>
// Set the checkbox to be checked dynamically
document.getElementById("dynamicCheckbox").checked = true;
</script>
🧠 How it Works
In this script, the checked
attribute is set to true for a checkbox with the id dynamicCheckbox.
This can be beneficial when you want to dynamically preselect a checkbox based on specific user actions.
🏆 Best Practices
- Use the
checked
attribute to set the initial state of checkboxes or radio buttons when you want them to be selected by default. - If you need to change the state dynamically based on user interactions, leverage JavaScript to manipulate the checked property.
- Keep in mind that the
checked
attribute is specific to checkboxes and radio buttons. For other input types, consider alternative attributes or methods.
🎉 Conclusion
The checked
attribute is a simple yet powerful tool for controlling the initial state of checkboxes and radio buttons in HTML.
By understanding how to use this attribute, you can enhance the default behavior of your forms and improve user experience.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML checked Attribute), please comment here. I will help you immediately.