Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an option to a select element in second place

Tags:

jquery

I search to know how place a new select element at the second place of a select list

The hierarchy:

  • first : white element
  • second : my new element (there : Add)
  • next : my email list

My function :

function classAppend(){
    $('#email').append(
        $('<option></option>').val('').addClass('new_address_mail').html("Add")
    );
}

Some one know what is the option?

like image 358
Uub Avatar asked May 29 '12 12:05

Uub


3 Answers

Try this:

$('#email option:first').after($('<option />', { "value": '', text: 'My new option', class: 'new_address_mail' }));
like image 165
Phil.Wheeler Avatar answered Oct 13 '22 09:10

Phil.Wheeler


You probably have to select the first child of #email and insert your element with insertAfter:

$('<option></option>')
   .val('')
   .addClass('new_address_mail')
   .html("Add")
   .insertAfter($('#email').children().first());
like image 42
lonesomeday Avatar answered Oct 13 '22 11:10

lonesomeday


$("<option value='x'>newly added</option>").insertAfter($("select option:first"));​

DEMO

like image 33
Ranganadh Paramkusam Avatar answered Oct 13 '22 09:10

Ranganadh Paramkusam