Set selected="selected" for the option you want to be the default.
3
In case you want to have a default text as a sort of placeholder/hint but not considered a valid value (something like "complete here", "select your nation" ecc.) you can do something like this:
Choose here
One
Two
Three
Four
Five
Complete example:
1
2
3
4
5
I came across this question, but the accepted and highly upvoted answer didn't work for me. It turns out that if you are using React, then setting selected doesn't work.
Instead you have to set a value in the tag directly as shown below:
Apple
Banana
Cranberry
You can do it like this:
1
2
3
4
5
Provide "selected" keyword inside the option tag, which you want to appear by default in your drop down list.
Or you can also provide attribute to the option tag i.e.
3
if you want to use the values from a Form and keep it dynamic try this with php
$selected = $_POST['select'];
?>
>1
>2
I prefer this:
Choose here
One
Two
Three
Four
Five
'Choose here' disappears after an option has been selected.
An improvement for nobita's answer. Also you can improve the visual view of the drop down list, by hiding the element 'Choose here'.
Choose here
One
Two
Three
Four
Five
Another example; using JavaScript to set a selected option.
(You could use this example to for loop an array of values into a drop down component)
// Get the select element
var select = document.getElementById("yourDropDownElementId");
// Create a new option element
var el = document.createElement("option");
// Add our value to the option
el.textContent = "Example Value";
el.value = "Example Value";
// Set the option to selected
el.selected = true;
// Add the new option element to the select element
select.appendChild(el);
The selected attribute is a boolean attribute.
When present, it specifies that an option should be pre-selected when the page loads.
The pre-selected option will be displayed first in the drop-down list.
Volvo
Saab
VW
Audi
If you are in react you can use defaultValue as attribute instead of value in the select tag.
If you are using select with angular 1, then you need to use ng-init, otherwise, second option will not be selected since, ng-model overrides the defaul selected value
Name
Stars
Language
I used this php function to generate the options, and insert it into my HTML
# code to output a set of options for a numeric drop down list
# parameters: (start, end, step, format, default)
function numericoptions($start, $end, $step, $formatstring, $default)
{
$retstring = "";
for($i = $start; $i <= $end; $i = $i + $step)
{
$retstring = $retstring . '
$retstring = $retstring . 'value="' . sprintf($formatstring,$i) . '"';
if($default == $i)
{
$retstring = $retstring . ' selected="selected"';
}
$retstring = $retstring . '>' . sprintf($formatstring,$i) . '
';}
return $retstring;
}
?>
And then in my webpage code I use it as below;
If $endmin is created from a _POST variable every time the page is loaded (and this code is inside a form which posts) then the previously selected value is selected by default.
This code sets the default value for the HTML select element with PHP.
$default = 3;
$nr = 1;
while($nr < 10){
if($nr == $default){
echo "". $nr ."";
}
else{
echo "". $nr ."";
}
$nr++;
}
?>
value attribute of tag is missing, so it doesn't show as u desired selected. By default first option show on dropdown page load, if value attribute is set on tag.... I got solved my problem this way
You can use:
Some Value
instead of,
Some Value
both are equally correct.