5

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

Aharon Muallem
  • 1,143
  • 3
  • 13
  • 20
  • probably duplicate: http://stackoverflow.com/questions/317095/how-do-i-add-options-to-a-dropdownlist-using-jquery – Samich Sep 08 '11 at 10:06
  • for adding at the begining use `prepend()` instead of `append` – Samich Sep 08 '11 at 10:07
  • 1
    Possible duplicate of [How can I add an option in the beginning?](https://stackoverflow.com/questions/5934113/how-can-i-add-an-option-in-the-beginning) – faintsignal Feb 04 '19 at 15:56

1 Answers1

12

You are so close! :)

   .prepend( content, [content] )

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

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

Marco Johannesen
  • 13,084
  • 6
  • 31
  • 36