31

I have a HTML select like this:

<select>
    <option>a</option>
    <option>b</option>
    <option>c</option>
</select>

and I have a variable named temp in my JavaScript:

var temp = "a";

Now I want to set the value of the option that is equal to temp as default value for my select.

How can I do it?

Sébastien
  • 11,860
  • 11
  • 58
  • 78
Saman
  • 5,173
  • 6
  • 17
  • 17

6 Answers6

73

You first need to add values to your select options and for easy targetting give the select itself an id.

Let's make option b the default:

<select id="mySelect">
    <option>a</option>
    <option selected="selected">b</option>
    <option>c</option>
</select>

Now you can change the default selected value with JavaScript like this:

<script>
var temp = "a";
var mySelect = document.getElementById('mySelect');

for(var i, j = 0; i = mySelect.options[j]; j++) {
    if(i.value == temp) {
        mySelect.selectedIndex = j;
        break;
    }
}
</script>

Also we can use "text" property of i to validate:

if(i.text == temp)

See it in action on codepen.

IngVik
  • 3
  • 2
Sébastien
  • 11,860
  • 11
  • 58
  • 78
  • 3
    +1 for the most complete answer and working example provided! – Automate This Oct 26 '13 at 20:49
  • 11
    +1 for using javascript; the "accepted" answer uses jQuery. – akalata Apr 29 '14 at 14:46
  • 3
    This is definitely the right answer. The codepen has an error though - uses a , where it should have a ; this one works though http://codepen.io/anon/pen/LEVjvr – ovann86 Nov 26 '14 at 01:09
  • 2
    Thank You for pointing this out @ovann86. I updated the codepen example. – Sébastien Nov 26 '14 at 21:56
  • Could you extend your answer for Html helpers? For example: @Html.DropDownListFor(Function(Model) Model.ReportId, New SelectList(Model.allReports, "ReportID", "ReportName", New With {.class = "form-control", .id = "ReportID"}), "Select Report....") – JoshYates1980 May 03 '16 at 16:49
29

Note: this is JQuery. See Sébastien answer for Javascript

$(function() {
    var temp="a"; 
    $("#MySelect").val(temp);
});

<select name="MySelect" id="MySelect">
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
</select>
Automate This
  • 30,726
  • 11
  • 60
  • 82
7

You should define the attributes of option like selected="selected"

<select>
   <option selected="selected">a</option>
   <option>b</option>
   <option>c</option>
</select>
Erik Honn
  • 7,576
  • 5
  • 33
  • 42
5

Simplay you can place HTML select attribute to option a like shown below

Define the attributes like selected="selected"

<select>
   <option selected="selected">a</option>
   <option>b</option>
   <option>c</option>
</select>
Ashraf.Shk786
  • 618
  • 1
  • 11
  • 23
Naveen Kumar Alone
  • 7,536
  • 5
  • 36
  • 57
2

you can define attribute selected="selected" in Ex a

0

You could use...

<option <?= ($temp == $value) ? "SELECTED" : "" ?> >$value</opton>

Edit: I thought I was looking at PHP questions... Sorry.

Gavin
  • 2,123
  • 1
  • 15
  • 19