A vanilla JavaScript multi-select plugin that transforms the normal multi-select element into a user-friendly tags input. Show The users are allowed to select multiple options from the dropdown select. Click the ‘X’ button to remove the selected option. In this tutorial, we are going to learn how to select multiple options in a dropdown list with JavaScript. Basically, we are going to make two buttons, one is a drop-down button in which we provide more than one option, and another is a normal button connected to the multiFunc() function with an id of the dropdown button, when we click this button then we are able to select multiple options in the drop-down button. In this tutorial we are using two properties of JavaScript one is DOM property i.e. document.getElementById and another one is multiple properties use to choose more than one option at a time. SyntaxNow let’s move to see the syntax to select multiple options in a dropdown list with JavaScript − function multipleFunc() { document.getElementById("mySelect").multiple = true; } In the above syntax, we have gotten the function name “multipleFunc”. As the name suggests this function allows us to choose more than one option at a time. In this function, we use DOM using the id of a drop-down button with multiple properties and assign it to true. And this “multipleFunc” is connected to a normal button, so when we click on this button to activate the “multipleFunc” function to select more than one option in the dropdown button. AlgorithmWe have seen the syntax above to select multiple options in a dropdown list with JavaScript, now we are going to see the complete approach step by step −
We have seen the syntax and algorithm to select multiple options in a dropdown list with JavaScript, now let’s take an example to implement the above-discussed steps. ExampleIn this example, we will take a form with a dropdown button and a normal button connected to the multipleFunc, and then with the help of JavaScript properties we are able to select multiple options in a dropdown list.
In the above code first, we create a form in which we create two buttons. First, is the dropdown button using the select tag of HTML with its own id, and second is normal using the input tag in which we pass type, onclick, and value. When we click the second button it is called the function “multiFunc”, it is connected by the onclick function. multiFunc is created to allow the user to choose multiple options at a time so in this function, we are using DOM property document.getElementById and id of the drop-down button with ‘multiple’ property and assign it to value ‘true’. You can see the output images, in the first image option drop-down button and normal button but we are not able to select more than one option at a time in the drop-down button then we click on the select multiple options button we are able to select multiple options in the drop-down button as you can see in the second picture. ConclusionIn this tutorial, we learned to select multiple options in a dropdown list with javascript. Further, we learned how to create a form with a dropdown button and normal button using input connected to the ‘multipleFunc’ function by onclick property in that function we are using the dom property of javascript “document.getElementById” and multiple properties of javascript which allow us to choose more than one option at a time. A drop-down thus works as an alternative and in specific cases, a better option, as compared to radio buttons and checkboxes. Wait, what? Did we say an alternative to both radio buttons and checkboxes? But, it should have been just the radio button, shouldn’t it? A radio button does not allow more than one selections and a checkbox, on the other hand, allows one or more than one selections. A user can select only one option from a drop-down list and therefore it resembles just the radio button in its functionality and not the checkboxes, right? Well, this is where the concept of multiple selections in a select box comes into play. How can I allow users to select multiple options from a select box?From a web designer’s perspective, allowing the user to choose multiple options from a select box is fairly simple and straightforward. We just need to add the “multiple” attribute to the <select> element. Now, in order to select multiple options, you have to add a size attribute also to the select element. This will make the select box look like a box rather than just the drop-down. The user has to hold down the Ctrl key (in Windows) and Command key (on Mac) to select the choices.
Note these points
Enhancing the DropdownAs you can see, the native HTML select box is quite limited especially for choosing multiple options. This is where JavaScript and the highly effective libraries come into the fray. The select2 jQuery component gives the designer the power to customize the overall layout of the select box along with incorporating high utility options such as searching, dynamic loading, tagging, among others. Does select2 support multiple selections?Select2 supports multiple selections in drop-down list. To enable multiple selections, just include ‘multiple’ as an attribute. The advantage here is that the user does not have to press Ctrl or Command key to select multiple options.
Select2 also adds other enhancements as well. For Example, in order to limit the number of choices the user can make, just add maximumSelectionLength in the Select2 options like this: How do I select multiple options in a dropWindows: We need to hold down the CTRL button to select multiple options. Mac: We need to hold down the command button to select multiple options.
How do I select multiple options from a dropBut in this case, multiple options can be selected by holding down the control (ctrl) button of the keyboard. Instead of using the multiple attributes in HTML, you can use jQuery to make the multi-select dropdown more user-friendly and add the checkbox to each option in the multi-select dropdown.
|