Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set the default value for an HTML <select> element?

I thought that adding a "value" attribute set on the <select> element below would cause the <option> containing my provided "value" to be selected by default:

<select name="hall" id="hall" value="3">    <option>1</option>    <option>2</option>    <option>3</option>    <option>4</option>    <option>5</option>  </select>

However, this did not work as I had expected. How can I set which <option> element is selected by default?

like image 415
Jichao Avatar asked Aug 19 '10 01:08

Jichao


People also ask

How do you set the value of a select element?

Use the value property to set the value of a select element, e.g. select. value = 'new value' . The value property can be used to set or update the value of a select element. To remove the selection, set the value to an empty string.

How do you change the selected value in HTML?

To change the selected option of an HTML select element with JavaScript, we can set the value property of the select element. to add the select element. document. getElementById("sel").

How do I change the default value in drop down?

Use the <select> element for this, which is a select box, also called drop down box, with option to list down items. Also, you can set the default value from the dropdown list of items in HTML forms. For that, add selected in the <option> tag for the value you want to preselect.


1 Answers

Set selected="selected" for the option you want to be the default.

<option selected="selected"> 3 </option> 
like image 176
Borealid Avatar answered Sep 23 '22 02:09

Borealid