Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First option of dropdown not an option; force to use other options [duplicate]

<select name="name">
    <option>Select an option.</option><br/>
    <option>A</option><br/>
    <option>B</option><br/>
    <option>C</option><br/>
</select>

HTML5 has required="required", and this is the kind of effect I want with the dropdown above. I don't want the user to select "Select an option", only "A", "B", or "C". I know I could set a default value of A and just remove the "Select an option" option entirely, but I don't want users to click haphazardly without reading the options available.

Can this be done with HTML and/or JS?

like image 922
gator Avatar asked Nov 22 '13 04:11

gator


People also ask

How do I get the first select option?

Select the <select> element using JQuery selector. This selector is more specific and selecting the first element using option:nth-child(1). This will get access to the first element (Index starts with 1).

How can multiple options be selected in drop-down?

To select multiple options in a drop-down list, use the multiple properties. It allows you to select more than one option while pressing CTRL key.

How do I change a selection based on another dropdown?

Create a local variable say type that will hold any of the array based on the dropdown selected. Create a function that will be called whenever an option from the dropdown is selected, this function will change the state variable so that the value of type can be determined dynamically.


1 Answers

edit: this code is only to force users to choose a valid option, instead of the one by default... validation for submit/change/etc should be added

You should just disable the first option and setting it as selected by default:

<select name="name">
  <option disabled="disabled" selected="selected">Select an option.</option>
  <option>A</option>
  <option>B</option>
  <option>C</option>
</select>

That should do the trick, demo here http://jsfiddle.net/pixshatterer/Q27HX/

like image 152
pixshatterer Avatar answered Oct 16 '22 08:10

pixshatterer