Add option to select item in second place

I am looking to find out how to place a new selection item in second place in the selection list

Hierarchy:

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

My function:

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

Does anyone know what an option is?

+5
source share
3 answers

Try the following:

$('#email option:first').after($('<option />', { "value": '', text: 'My new option', class: 'new_address_mail' }));
+12
source

You probably need to select the first child element #emailand insert your element with insertAfter:

$('<option></option>')
   .val('')
   .addClass('new_address_mail')
   .html("Add")
   .insertAfter($('#email').children().first());
+1
source
$("<option value='x'>newly added</option>").insertAfter($("select option:first"));​

Demo

+1
source

All Articles