How to mimic required for select field [duplicate] - html

This question already has answers here:
Can I apply the required attribute to <select> fields in HTML?
(13 answers)
Closed 8 days ago.
I want to mimic required for select field too. The required field works for select field if the first option is empty. But still i cant see the box which says "please fill out this field"
Is there is a hack or a way i can add the required box to the select field similar like what we have for input field in html5

required attribute works well on <select>. If you want to force user to make a choice here, create an option with blank value (fiddle).
<select name="choice" required>
<option value="">None</option>
<option value="1">One</option>
<option value="2">Two</option>
</select>

Related

In HTML: Different <select> referencing the same list of options

Is it possible for <select>-lists to reference the same list of options, similar to <input> with <datalist>?
I generate a list of several entries, where (among other things) the user selects a value of a dropdownlist. The options in this list are the same for each entry, so I would prefer it, if the list of options doesn't need to be re-added for each dropdownlist.
I can't use <input> with <datalist>, since the user may only choose from available entries.
you could do this using jquery easily,
<datalist id="mylist">
<option value="a">
<option value="b">
<option value="b">
</datalist>
<select class="someSelect">
<select class="someSelect">
$(".someSelect").html( $("#mylist").html() );
this would replace all your select list from the datalist
This is not realy the good answer. There is a big difference between 'datalist' and 'select' which for as far as I read till yet stays unspoken : in a select the 'value' can be different from the visualized 'innerHTML', which is not the case in a datalist. So what we need is a kind of 'select' with an attribute like 'selectlist="countries' the selectlist then would look like this :
<selectlist>
<option value='1'>Belgium</option>
<option value='2'>France</option>
</selectlist>
and can be reused in more then one 'select' and send the value back to the server instead of the innerHTML.

Html select with options as textboxes

I am writing an select drop down in html which has
<option><input type="text"></option>
But when I am running that program the textbox is coming out of the dropdown,the purpose of inserting text box in select is that , If a user is unable to find its desired option . He will be able to add a new one.
This is the select code. You can see alive demo running this code http://jsfiddle.net/_nikhilagrawal/eJ7k6/3/. Why this input type text is coming outside the select. Please have a look.
<select name="color">
<option>pick a color</option>
<option value="red">RED</option>
<option value="blue">BLUE</option>
<option><input type="text" name="color" /></option>
</select>
This is the output of the drop down.
<option> elements cannot have anything other than text inside them.
If you want a widget that combines text inputs with a dropdown menu then you will have to build it using JavaScript.
You can not write
<input type="text" name="color" />
inside <option></option> tag.
You should add textbox nearest to selectbox If a user is unable to find its desired option,user can add new one. or you need to customize your select box using css+jquery

How to input text in HTML Select Tag?

I want to make input option in select tag so user can choose between options or insert different value.
Is it possible?
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
**<insert user value>**
</select>
HTML solution with "list" attribute:
<input type="text" name="city" list="citynames">
<datalist id="citynames">
<option value="Boston">
<option value="Cambridge">
</datalist>
You will have to use javascript to get the additional value. Check this post for some example code:
Jquery dynamically update "other" option in select
Select elements can't contain anything other than option or optgroup elements. Here's a ref to the spec http://www.w3.org/TR/html5/forms.html#the-select-element
You may be better off adding an option for "other" in your dropdown and then using JS to detect for that choice to dynamically show an input (below the dropdown) for a custom value.
Position an input box over the select box and remove the border of the input box. Make the length of the input box shorter than the select box so that the select box may still be used at its end.
Use the oninput event to detect input being entered in the input box. On each keystroke check for a continuing match in the select box. When a match no longer exists there is no further need for the select box.
The server will expect to receive both the text box input, if any, and the select box input, if any, and should use the select value if provided otherwise the input value if provided.

<option selected = "selected"> issues in IE9 [duplicate]

This question already has answers here:
How to change a <select> value from JavaScript
(12 answers)
Closed 9 years ago.
I'm having issues with setting a default value in a dropdown menu in IE9. It seems to work on other browsers. All my searches yielded people trying to use JQuery to set that attribute. But I'm not using JQuery at all. Here's a sample:
<select id="selectType" name="selectType">
<option label="1" selected="selected">1</option>
<option label="2">2</option>
<option label="3">3</option>
</select>
I forgot to add that the problem is that it's displaying a blank field as the default instead of "1".
That should work just fine, although personally I would take into account that selected is a boolean attribute and therefore its presence is all that is required:
<option label="1" selected>1</option>
That being said, I'm not sure label is a valid attribute there. I'm fairly sure they're inteded for <optgroup> tags. Try removing that attribute.

<SELECT multiple> - how to allow only one item selected?

I have a <SELECT multiple> field with multiple options and I want to allow it to have only one option selected at the same time but user can hold CTRL key and select more items at once.
Is there any way how to do it? (I don't want to remove 'multiple').
Just don't make it a select multiple, but set a size to it, such as:
<select name="user" id="userID" size="3">
<option>John</option>
<option>Paul</option>
<option>Ringo</option>
<option>George</option>
</select>
Working example:
https://jsfiddle.net/q2vo8nge/
If the user should select only one option at once, just remove the "multiple" - make a normal select:
<select name="mySelect" size="3">
<option>Foo</option>
<option>Bar</option>
<option>Foo Bar</option>
<option>Bar Foo</option>
</select>
Fiddle
Why don't you want to remove the multiple attribute? The entire purpose of that attribute is to specify to the browser that multiple values may be selected from the given select element. If only a single value should be selected, remove the attribute and the browser will know to allow only a single selection.
Use the tools you have, that's what they're for.
You want only one option by default, but the user can select multiple options by pressing the CTRL key. This is (already) exactly how the SELECT multiple is meant to behave.
See this: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_select_multiple
Can you please clarify your question?
<select name="flowers" size="5" style="height:200px">
<option value="1">Rose</option>
<option value="2">Tulip</option>
</select>
This simple solution allows to obtain visually a list of options, but to be able to select only one.
I had some dealings with the select \ multi-select
this is what did the trick for me
<select name="mySelect" multiple="multiple">
<option>Foo</option>
<option>Bar</option>
<option>Foo Bar</option>
<option>Bar Foo</option>
</select>
I am coming here after searching google and change thing at my-end.
So I just change this sample and it will work with jquery at run-time.
$('select[name*="homepage_select"]').removeAttr('multiple')
http://jsfiddle.net/ajayendra2707/ejkxgy1p/5/
Late to answer but might help someone else, here is how to do it without removing the 'multiple' attribute.
$('.myDropdown').chosen({
//Here you can change the value of the maximum allowed options
max_selected_options: 1
});