Skip to content Skip to sidebar Skip to footer

How Can I Add An Option In The Beginning?

I have html select, I need to

Solution 1:

First, you mean append, rather than appendTo. append adds the content of the parameter to the selection; appendTo appends the selection to the element designated in the parameter.

Second, perhaps intuitively, you can use prepend:

$('#myselect').prepend($('<option>my-option</option>')); 

Solution 2:

In POJS you'd use insertBefore(), e.g.

var select = document.getElementById('mySelect');
var opt = new Option('', 'my-option');
select.insertBefore(opt, select.firstChild);

Solution 3:

Use prependTo()

$('#myselect').prependTo($('my-option'));//typo? 
You might have meant this

$('#myselect').prepend($('<option>my-option</option>'));  

http://api.jquery.com/prependTo

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

But your code is actually the other way around. This might be a typo. In any case, the methods available are prepend() and prependTo()


Post a Comment for "How Can I Add An Option In The Beginning?"