Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

HTML Basic

HTML Reference

HTML multiple Attribute

Posted in HTML Tutorial
Updated on Nov 19, 2024
By Mari Selvan
👁️ 58 - Views
⏳ 4 mins
💬 1 Comment
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:

multiple.html
Copied
Copy To Clipboard
<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:

multiple.html
Copied
Copy To Clipboard
<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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
9 months ago

If you have any doubts regarding this article (HTML multiple Attribute), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy