HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- 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 Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML selected Attribute
Photo Credit to CodeToFun
🙋 Introduction
The selected
attribute is a crucial feature in HTML, particularly when working with dropdown lists or <select> elements.
This attribute allows developers to predefine an option as selected, providing a default choice for users when they interact with the dropdown.
🎯 Purpose of selected
The primary purpose of the selected
attribute is to specify the initially selected option within a <select> element.
This is beneficial when you want to highlight a default choice or prefill the dropdown with a specific value.
💎 Values
The selected
attribute is a Boolean attribute, meaning it doesn't require a value. Its presence alone indicates that the option is selected. Here's an example:
<select>
<option value="option1" selected>Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select>
🧠 How it Works
In this example, "Option 1" is set as the default selected option due to the presence of the selected
attribute.
📄 Example
Let's consider a more practical scenario where the selected
attribute is used within a form:
<form>
<label for="car">Select Your Car:</label>
<select id="car" name="car">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes" selected>Mercedes</option>
<option value="audi">Audi</option>
</select>
<input type="submit" value="Submit">
</form>
🧠 How it Works
In this case, "Mercedes" is initially selected when the form loads.
🔄 Dynamic Values with JavaScript
You can dynamically set the selected
attribute using JavaScript.
This is useful when you want to update the selected option based on user interactions or certain conditions. Here's a simple example:
<script>
// Dynamically set selected option based on a condition
var conditionMet = true;
if (conditionMet) {
document.getElementById("dynamicSelect").value = "saab";
}
</script>
🧠 How it Works
In this script, if the conditionMet variable is true, the option with the value "saab" in the dropdown with the id "dynamicSelect" will be dynamically selected.
🏆 Best Practices
- Use the
selected
attribute to provide a default option for users, especially when one choice is more commonly selected. - Be mindful of accessibility considerations when preselecting options. Ensure that the default selection makes sense in the context of the form.
- Test your forms across different browsers to ensure consistent behavior.
🎉 Conclusion
The selected
attribute is a valuable tool for defining default selections in HTML dropdowns.
By leveraging this attribute, you can enhance the user experience and guide users towards common or recommended choices.
👨💻 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 selected Attribute), please comment here. I will help you immediately.