2
I have a question about the "select" tag, if it is possible that it has a visible title without being part of the options?
2
I have a question about the "select" tag, if it is possible that it has a visible title without being part of the options?
3
You can define a select
, having a first option
marked as disabled and selected at the same time.
Behold:
<select>
<option disabled selected>Cars</option>
<option>Volvo</option>
<option>Mercedes Bens</option>
</select>
When you select the input
, you can remove it too using the event onmounsedown
.
Behold:
<select onmousedown="document.querySelector('#fake').remove()">
<option disabled selected id="fake">Cars</option>
<option>Volvo</option>
<option>Mercedes Bens</option>
</select>
Note that, in this last example, I added an id to option
to make it easier to find it via querySelector
.
You can also set as in the first example, but adding a display:none
in the first option
.
<select>
<option disabled selected style="display: none">Cars</option>
<option>Volvo</option>
<option>Mercedes Bens</option>
</select>
In this last example, I recommend using a required
in his select
. There, the others option
would have to have some value in value
, leaving only the first with nothing in value
.
Behold:
<form>
<select required>
<option disabled selected style="display: none;" value="">Cars</option>
<option value="volvo">Volvo</option>
<option value="mercedes">Mercedes Bens</option>
</select>
<button type="submit">Submit</button>
</form>
Observing: If you want to set a CSS for the first option
to differentiate from others, it is possible. However in the tests I did only worked when you click on select
(and I don’t know if it works in other browsers).
Tested in Google Chrome:
<select required>
<option selected style="color: #555; background-color: #ddd">Cars</option>
<option value="volvo">Volvo</option>
<option value="mercedes">Mercedes Bens</option>
</select>
0
<select>
<optgroup label="Swedish Cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</optgroup>
</select>
Source: W3schools
Friend thanks for the attention, but in this way the "select" starts with a "Volvo" option, I would like it to be visible for example "Cars" and in the brand options, but that "Cars" was not an option.
The @Nilsonueahara answer is correct, the optgroup represents only a text not selectable within a select.
@joaopaulosantosalmeida but I believe that is not what was asked in the question.
Browser other questions tagged html html-select
You are not signed in. Login or sign up in order to post.
I think this is as close as I wish, cool if I could put it on Hidden on the list?
– MagicHat
This 3rd is Peter, vlw...
– MagicHat
@Magichat looks at the 4th example. This is for the form not to leave the Submit, if the guy has not selected a valid item. No Javascript
– Wallace Maxters
Mass, required...show.. Only bad pq, not very comprehensive in terms of browsers.. At this point as the form will have anyway a kind of validation, then I will appeal pro js, for this purpose
– MagicHat
Your second code keeps giving me a mistake: "Uncaught Typeerror: Cannot read Property 'remove' of null" - The error does not occur the first time you make the selection, but occurs from the second on.
– Victor Stafusa
@Victorstafusa I think this is because the element has been removed. Maybe it’s really better to use the "other options"
– Wallace Maxters