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 multiple Attribute
Photo Credit to CodeToFun
🙋 Introduction
The multiple
attribute is a powerful feature in HTML that allows users to select multiple options in a form element.
Typically used with the <select> element, this attribute provides a convenient way to create dropdown lists where users can choose more than one option.
🎯 Purpose of multiple
The primary purpose of the multiple
attribute is to enable multiple selections within a dropdown list. This can be beneficial in situations where users need to choose multiple items from a list without the need for additional UI elements.
💎 Values
The multiple
attribute has a single value:
- multiple: This value is assigned to the
multiple
attribute to indicate that multiple selections are allowed. If the attribute is present, users can select multiple options by holding down the Ctrl key (or Command key on macOS) while clicking on the desired options.
📄 Example
Let's look at a simple example of how to use the multiple
attribute with a <select> element:
<form>
<label for="fruitSelection">Select your favorite fruits:</label>
<select id="fruitSelection" name="fruits" multiple>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="orange">Orange</option>
<option value="grape">Grape</option>
</select>
<input type="submit" value="Submit">
</form>
🧠 How it Works
In this example, the multiple
attribute is applied to the <select> element, allowing users to select multiple fruits simultaneously.
🔄 Dynamic Values with JavaScript
You can dynamically set the multiple
attribute using JavaScript, providing flexibility in handling user interactions or changing requirements. Here's an example:
<script>
// Dynamically set multiple attribute for a select element
document.getElementById("dynamicSelect").multiple = true;
</script>
🧠 How it Works
In this script, the multiple
attribute is dynamically set to true for a <select> element with the id "dynamicSelect." This allows users to select multiple options in the dropdown list.
🏆 Best Practices
- Use the
multiple
attribute when you want to allow users to select multiple options from a list. - Provide clear instructions to users about how to make multiple selections, especially if the interface design is not immediately intuitive.
- Test your forms across different browsers to ensure consistent behavior with the
multiple
attribute.
🎉 Conclusion
The multiple
attribute enhances user interactivity in HTML forms, enabling the selection of multiple options with ease.
By understanding and utilizing this attribute, you can create more versatile and user-friendly forms.
👨💻 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 multiple Attribute), please comment here. I will help you immediately.