user588181
user588181

Reputation: 147

Javascript and Select box

I have select box with options starts from 0 index.So here I want to add option tag at zero index without deleting or replacing the original zero index option element.

Thanks In advance.

Upvotes: 0

Views: 134

Answers (2)

lonesomeday
lonesomeday

Reputation: 237865

var select = document.getElementById('myselect');
var newoption = document.createElement('option');
newoption.value = 0; // or whatever
newoption.innerHTML = '0'; // or whatever
select.insertBefore(newoption, select.options[0]);

Upvotes: 1

bleepzter
bleepzter

Reputation: 9985

I don't think this is possible. Any javascript library that allows you to manipulate the inner html of the <select> will have to copy all options to a temp array, delete them, add the one you want, and finally add the options in the temp array.

You maytry this with JQuery:

$('.selector').prepend('<option value="value">text</option>') but i doubt it will work.

Upvotes: 0

Related Questions