Uub
Uub

Reputation: 135

Add an option to a select element in second place

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

The hierarchy:

My function :

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

Some one know what is the option?

Upvotes: 6

Views: 6845

Answers (3)

Ranganadh Paramkusam
Ranganadh Paramkusam

Reputation: 4358

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

DEMO

Upvotes: 1

Phil.Wheeler
Phil.Wheeler

Reputation: 16848

Try this:

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

Upvotes: 16

lonesomeday
lonesomeday

Reputation: 237817

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());

Upvotes: 2

Related Questions