Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add option to select menu at the beginning of the menu

How can I add an option to a menu at the begining of the select menu?

I know the append will add the end

   SomeSelectMenu.append(NewElement);

But how will I add at the beginning? Here is an example:

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

Please specify an answer with Jquery and one without (if possible) Thanks

like image 901
Aharon Muallem Avatar asked Sep 08 '11 10:09

Aharon Muallem


1 Answers

You are so close! :)

   .prepend( content, [content] )

$('select').prepend('<option value="i-came-first">Hey!</option>');

http://api.jquery.com/prepend/

like image 174
Marco Johannesen Avatar answered Sep 22 '22 19:09

Marco Johannesen