How to fix number of visible items in HTML dropdown and allow multiple selections ?

Drop down menu provides a list of options to choose from. The HTML <select> tag is used to create a drop down list.

When the number of options in a drop-down list is large, fixing the number of visible items may be useful. This can be done by using the “size” attribute of <select> tag.  

In the following example, 2 items are visible at a time because the value of the size attribute is set to 2.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    
<body>
   Choose a language:<br>
   <select id="language" size="2" >
       <option value="C">C</option>
       <option value="C++">C++</option>
       <option value="Java">Java</option>
       <option value="Python">Python</option>
       <option value="R">R</option>
       <option value="HTML">HTML</option>
       <option value="JavaScript">JavaScript</option>
   </select>
</body>
  
</html>

chevron_right


Output:



In the above example, only one item can be selected from the list. To enable multiple selections, a “multiple” attribute is used. In the following example, multiple options can be selected by holding the Ctrl button (Windows) or Command (Mac):

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
  
<body>
   Choose a language:<br>
  
   <select id="language" size="4" multiple>
       <option value="C">C</option>
       <option value="C++">C++</option>
       <option value="Java">Java</option>
       <option value="Python">Python</option>
       <option value="R">R</option>
       <option value="HTML">HTML</option>
       <option value="JavaScript">JavaScript</option>  
   </select>
  
   <p>
       Hold ctrl (windows) or Command 
       (Mac) to select multiple option
   </p>    
</body>
  
</html>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.