Question

Does anyone know how to set the first option value of a select using jquery? For example i have the following select tag.

<select id = "my-id">
    <option value=""></option>
</select>

I want to set the value. So far I have unsuccessfully attempted the following.

$("select#my-id").first().val("hello world");

document.getElementById('my-id').value="hello world";

$("select#my-id option")
         .each(function() {
                 this.text = query.eguide;
                 this.value =query.eguide;});
Was it helpful?

Solution

$("#my-id option:first").attr("value", "blahblah");

or (thanks to @Tom)

$("#my-id option:first").val("blahblah");

This will set the val attribute to blahblah. Obviously changing blahblah to whatever you need.

OTHER TIPS

And a solution with plain js you can try:

document.getElementById("my-id").options[0].value="TEST";

DEMO

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top