Option required in multiple select - html

I have a select like this :
<select name="cars" multiple>
<option value="volvo" selected>Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
I would like the option "Volvo" selected by default and required, but the user can unselect it if he wants. I would like that the user would not be able to unselect it.
This select is generated with PHP, so I can add an hidden field, but I want the user to know that this option is required. How can I do that?

Related

HTML Drop Down Menu - Default Value

I'm making a dropdown menu in HTML and was trying to figure out how to pre-select a drop down value when the page loads. I've tried the following, using the selected option to indicate what value I want to be the default upon load. Though, this doesn't seem to work and I still end up with the first option being the default. I'm sure it's really simple but is there
<select selected="3">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
Rather than manually put it in the <option value> tag, since these options are being generated via a loop, is there a way in the <select> tag to indicate what option should be defaulted? Thanks.
Use the selected property of the <option> you want selected by default:
<select>
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected>3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
You can set that property programatically, if you update your question to include how you are generating, I can update this to show you how.

Html multiple selected dropdown showing first item selected by default

I have made a drop down in which you can select multiple items but i want no item to be selected by default but when i come this page i get first item selected by default . I don't know how to set it as unselected at start.
<select>
<option disabled selected> </option>
<!-- Normal other options goes here -->
</select>
Add this extra option.
[or]
If you have JQuery/JavaScript, do the following
document.getElementById("dropdown").selectedIndex = -1;
$("#dropdown").prop("selectedIndex", -1);
No you cannot do without an extra option/without JS/JQuery. It is the default behaviour of dropdown
If you want no item to be selected you can just add this before your first option:
<option></option>
Update: For the multiple select: this should work fine
<select name="cars" multiple="multiple">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>

a option is selected without selected text in the option

When option is selected by default, without 'selected' text in it. How is it possible?.
I actually need a different option but I am not able to choose anything.
// set by default selected value using "selected" property of option
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel" selected>Opel</option>
<option value="audi">Audi</option>
</select>
// if you don't specify form select default option is first one "Volvo"
Default first one option is selected without "selected" property set beacuse that default behavior is that
If no "selected" attribute is provide, the first option in the select list will be selected by default. It will work this way in all browsers. Using an option with no text or value gives the appearance that nothing is selected:
<select>
<option value=""></option><!-- this will be selected by default, but the value will be empty string-->
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>

Store two values in a select form element separately

Does there exist a way to store two separate values in a select form element? For instance
<select>
<option value="">A + B</option>
</select>
I would like to store values A and B separately, but select them both with one option.
You can add two options:
<option value="a">A</option>
<option value="b">B</option>
Is this what you need?
Use a multiple select instead:
<select name="yourChoices[ ]" multiple>
<option value="A">A</option>
<option value="B">B</option>
</select>
Users are able to choose more than one option.
Here is an example: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple

hide selected value of a select list, when list is open

I have a dropdown select list, from which I can choose a country. the first option is: choose your country.
<select>
<option selected disabled value=''>Choose your country</option>
<option value="1">Canada</option>
<option value="2">France</option>
</select>
But when I click to open the list, I can see choose your country in the options. Is there a way to hide this option from the list, and only show it as a placeholder before any selection is made?
Hides the default option from appearing in the list:
http://jsfiddle.net/ZT2ZV/
IMO the better solution would be to use something like: http://harvesthq.github.io/chosen/
<select>
<option value='' disabled selected style='display:none;'>Choose your country</option>
<option value="1">Canada</option>
<option value="2">France</option>
</select>
Use display:none; on your first value:
<select>
<option value="" selected disabled style="display:none;">Choose your country</option>
<option value="2">Canada</option>
<option value="3">France</option>
</select>
try http://jsfiddle.net/uaF93/