Changing selected element of a select

Hi,

I’ve got a problem and i’ve been searching everywhere to solve it but i didn’t figure out a solution, probably that its somehow not supported or i don’t know.

Anyway, i need to change the selected item in a select dropdown list in html.
Obviously i want to change it with javascript. For example i’ve got tree items add/edit/delete and if the user presses add (button) the selected item will be add.

I hope i explained myself well… Any ideas friends ?

I hope i explained myself well

Not exactly. :slight_smile:

Why don’t you post some sample code so it is clearer to see what you want to do?

In any case, you can change the selected OPTION like this:

var opts = document.getElementById('the_select').getElementsByTagName('option');
for (var i = 0, j = opts.length; i < j; i++) {
  if (opts[i].value === 'something') opts[i].selected = true;
}

Hi,

I’m sorry i didn’t explain it well but your solution solved my problem. :slight_smile:

The thing was that i didn’t realise that i can select another item (dynamically) and the current selected gets unselected (when i’m tired its normal that i do such mistakes lol)

Anyway thanks a lot my friend, you saved me loads of time :wink: