How to set selected by default with HTML5 datalist - html

With HTML one can set the default selected item like this:
<option value="blaah" selected>
But how to do it with HTML5 and DataList select?

Not per se. Just set the value as normal.
<label for="ice-cream-choice">Choose a flavor:</label>
<input list="ice-cream-flavors" id="ice-cream-choice" name="ice-cream-choice" value="Strawberry" />
<datalist id="ice-cream-flavors">
<option value="Chocolate">
<option value="Coconut">
<option value="Mint">
<option value="Strawberry">
<option value="Vanilla">
</datalist>
Of course, clicks in the input will be filtered by the text already there, so users will have to delete it to get the list of suggestions back.

I had such a workaround that I added the default value into placeholder and then with the server-side language (I used PHP but you can do it with whatever you use) I check if the POST/GET is empty then it means default value.
Then I added into CSS:
::placeholder {
color: black;
}
So now it also looks right and doesn't give the user a feel that it's a placeholder and something needs to be done.

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?

How would one set titles and default values inside of a select list?

I'm looking for a way to set a default value (in a select list) that has to be switched out of in order for the 'required=""' attribute to be accepted and for the form to be submitted. In other words the default value can't stay as the chosen value. Also, how would one add titles to a select list that can not be chosen and are meant as a way of labeling the select options. Everything I am looking for is shown in the image below in case of confusion.
In other words the default value can't stay as the chosen value.
About this request, you could find this helpful.
<form action="/action_page.php">
<select id="mySelect" required onchange="changeValue()">
<option value="">Select a Country</option>
<option value="c1">Country 1</option>
<option selected value="c2">Country 2</option>
<option value="c3">Country 3</option>
<option value="c4">COuntry 4</option>
</select>
<input id="btnSubmit" type="submit" disabled>
</form>
<script>
function changeValue(){
if (document.getElementById("mySelect").value != "")
document.getElementById("btnSubmit").disabled = false;
}
</script>
If your selected value changes (just after the first time) and it's different from "", the submit button will be valid. This can also be accomplished with a flag instead of setting the property disable, it depends on what you need to do.
You could try checking with javascript if the value = ""
Example from w3Schools.com:
https://www.w3schools.com/js/js_validation.asp
Make sure you also validate on the server side! people could change the HTML / Javascript

How to make select elements required?

With input of type text the attribute required is available. It is not the case for select inputs. So how to make them required ?
FIDDLE
<form>
<select required>
<option></option><!--If this is selected require pop up will appear -->
<option>test</option><!--If this is selected form will be submitted -->
</select>
<input type="submit">
</form>
You can make them required by using html5 attribute required just like below.
<select required>
<option value="">select an option</option>
<option value="value1">Value 1</option>
<option value="value2">Value 2</option>
</select>
View Example in jsfiddle http://jsfiddle.net/88rXX/
Set a default value then on form submission check to see if that default value has changed.
If you're using the JQuery validation plug-in, try this Validate select box
You do have to remember though that just because it's validated client side, doesn't mean you shouldn't also check server side.
use it based on html 5. otherwise you can use any plugin

value attribute on <select> tag not selecting default option

Perhaps i'm misunderstanding here, but given the following html:
<select value="2">
<option value="1">Something</option>
<option value="2">Something else</option>
</select>
I would expect "Something else" to be the default selected option. However, it does not seem to be. Why is this, and what should I be doing differently?
You use selected attribute on an option element to specify default option.
<select>
<option value="1">Something</option>
<option value="2" selected="selected">Something else</option> // this is default
</select>
select elements do not have a value attribute.
The only way to have a default option is to have selected in the option tag.
<SELECT>
<OPTION>Something
<OPTION SELECTED>Something Else
React JS
Some coding implementations such as ReactJS allow you to use a value attribute with the <select> tag so this would be perfectly valid code:
<select value="2">
<option value="1">Something</option>
<option value="2">Something else</option>
</select>
So if you are seeing code examples like this it is likely because it is in React or other similar library/framework.
Of course, with this approach, typically you would want to specify the value in state, so that it is updateable.
HTML with Attribute Minimization:
However, if you are using purely HTML you must use the selected attribute in your <option> tag as follows:
<select>
<option value="1">Something</option>
<option value="2" selected>Something else</option>
</select>
HTML with Full Attribute Specification:
The above uses attribute minimization, but you can also specify the full form if you want:
<select>
<option value="1">Something</option>
<option value="2" selected="selected">Something else</option>
</select>
The <select> element does not have a value attribute so that is ignored. So, you have a single selection <select> and none of its <option> elements have the selected attribute, that means that the first <option> is taken as the default selection.
I know this post is quite old but in case anyone else is struggling with this you can implement the functionality you are looking for using jquery.
The full code using php would be something like this
PHP
if ($_SERVER['REQUEST_METHOD']== "POST") {
$thing = $_POST['things'];
} else {
$thing ="";
}
HTML
<select name='things' value="<?php echo $thing; ?>">
<option value="1">Something</option>
<option value="2">Something else</option>
</select>
JQUERY
$(function() {
$("select[value]").each(function() {
$(this).val(this.getAttribute("value"));
});
}); //end document ready
This will allow the select options chosen by the user to remain selected
after the page has re-loaded via post instead of returning
to the default values.
You have to use select attribute. in below code, a swift option will be selected by default
<select name="myCar" id="car">
<option value="ind">Indica</option>
<option value="swf" selected>Swift</option>
</select>

Does Native HTML have a ListBox element

Does native HTML have a listbox element? You know how it has a drop box element (called select), does it also have a listbox?
If not, do you know how I could place a list box in my Website.
One method, is to create a table & have each element detect the onclick event. But I dont want to make my own just yet. Are javascript widgets easy to use?
Use a select list:
<select multiple="multiple" size="2">
<option value="Whatever">One</option>
<option value="Other">Two</option>
</select>
#Myles has the select box which is correct, you can also allow multiple select options.
<select multiple="multiple">
<option value="opt1">Option 1</option>
<option value="opt2">Option 2</option>
<option value="opt3">Option 3</option>
</select>
Add the multiple attribute to a normal <select> and use the size attribute to determine how many rows you want shown. (If you don't set the size attribute, then all options will be visible.):
<select multiple="multiple" size="5">
See example.
I think what you need is the select tag, but set the selects attributes of multiple and size. Here is a reference http://www.w3schools.com/tags/tag_select.asp.
<select multiple='multiple' size='3'>
<option value='o1'>Red</option>
<option value='o2'>Green</option>
<option value='o3'>Blue</option>
</select>
At this moment the standards do not have a listbox element.
There's a web spec in progress about the <selectmenu> element which does have a listbox slot (not element). Possibly this could end up being back in the <select> element
If you want to read more about it:
https://open-ui.org/prototypes/selectmenu
https://css-tricks.com/the-selectmenu-element/