开发者

add option to select menu at the beginning of the menu

开发者 https://www.devze.com 2023-04-03 06:31 出处:网络
How can开发者_开发百科 I add an option to a menu at the begining of the select menu? I know the append will add the end

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


You are so close! :)

   .prepend( content, [content] )

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消