inherithandle
inherithandle

Reputation: 2664

jQuery : select box doesn't select default value

I've just learned how to change value of a select box. many posts on jQuery provides the statement like:

$("#selectBox").val("saab")

I tried the above statement, but value of select box won't be changed. Here's my code. I'd like to set value of the select box to Saab on html load.

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("#selectBox").val("saab")
    });
});
</script>
</head>
<body>

<select id="selectBox">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>


</body>
</html>

What's the problem with my code? Thank you in advance.

Upvotes: 0

Views: 162

Answers (1)

Vetrivel
Vetrivel

Reputation: 1149

Use the following code you will what you expect.

$(document).ready(function(){
    $("#selectBox").val("saab");
});

Note: In your code you close "})." 2 times, if you remove that one that itself working fine. When ever you working on javascript or jquery try to use firebug. you can easily find out the problem

Click here to know how to use Firebug

how to enable Firebug: click here

Upvotes: 2

Related Questions