a option is selected without selected text in the option - html

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>

Related

How to set default value of select in HTML?

I've made a select in html that looks like this <select name='select' id='select'><option value='val1'>Val1</option><option value='val2'>Val2</option></select>. Is there a way to set the default value of select to something like "pick a value"? Not as an option, but just the text of the select.
You can use the selected attribute within the option to select a default response. If you add the option disabled and hidden then this will be an invalid option.
<select name='select' id='select'>
<option value='val1' selected disabled hidden>Choose option</option>
<option value='val1'>Val1</option>
<option value='val2'>Val2</option>
</select>
Why not just have another option that doesn't have a value and give it the option of selected?
<select name='select' id='select'>
<option value='val1'>Val1</option>
<option value='val2'>Val2</option>
<option value='' selected>Choose Value</option>
</select>
Then in your php or whatever server side test to make sure you aren't accepting an empty value, or use js to not allow the form to submit if the value is blank?

IE style issue for select option have both selected and disabled attribute

I have an issue select box on ie only. If it have both the attribute selected and disabled together it breaks the default style. Is it have any solution for this problem?
Click on image for a larger version of the image.
Example code is attached with this description.
<select>
<option value="volvo" selected disabled>Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
they only workaround i could think of was to change the background-color of the disabled option to the default background-color a disabled button has, which is #f4f4f4
you should also add a conditional so the code will work only for IE . see here for more info > How to target only IE (any version) within a stylesheet?
option:disabled {
background-color:#f4f4f4;
}
<select>
<option value="volvo" selected disabled>Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>

Option required in multiple select

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?

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>

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/