How to create a listbox in HTML without allowing multiple selection?

HtmlListbox

Html Problem Overview


I don't have much experience in HTML. I am looking to create a simple listbox, but one of the requirements is to DISALLOW multiple selection. Most of the code for listboxes goes like this -

 <select name="sometext" multiple="multiple">
    <option>text1</option>
    <option>text2</option>
    <option>text3</option>
    <option>text4</option>
    <option>text5</option>
 </select>

But this allows for multiple selection.

Here, a similar question was asked, but the "best" answer has been downvoted. So I am not sure how else this could be done. Please help.

Html Solutions


Solution 1 - Html

Just use the size attribute:

<select name="sometext" size="5">
  <option>text1</option>
  <option>text2</option>
  <option>text3</option>
  <option>text4</option>
  <option>text5</option>
</select>

To clarify, adding the size attribute did not remove the multiple selection.

The single selection works because you removed the multiple="multiple" attribute.

Adding the size="5" attribute is still a good idea, it means that at least 5 lines must be displayed. See the full reference here

Solution 2 - Html

Remove the multiple="multiple" attribute and add SIZE=6 with the number of elements you want

you may want to check this site

http://www.htmlcodetutorial.com/forms/_SELECT.html

Solution 3 - Html

For Asp.Net MVC

@Html.ListBox("parameterName", ViewBag.ParameterValueList as MultiSelectList, 
 new { 
 @class = "chosen-select form-control"
 }) 

or

  @Html.ListBoxFor(model => model.parameterName,
  ViewBag.ParameterValueList as MultiSelectList,
   new{
       data_placeholder = "Select Options ",
       @class = "chosen-select form-control"
   })

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCodeBlueView Question on Stackoverflow
Solution 1 - HtmlaaroncatlinView Answer on Stackoverflow
Solution 2 - HtmlpollirrataView Answer on Stackoverflow
Solution 3 - HtmlDeshani TharakaView Answer on Stackoverflow