insert item to dropdownlist using javascript

J

Joe Fallon

I can add items to my dropdown list using javascript like this:

cbo.length = j+2;

cbo.options[j].text = "Some Text";

cbo.options[j].value = "MyValue";

cbo.options[j+1].text = "Some Other Text";

cbo.options[j+1].value = "MyOtherValue";

But the above code adds them to the bottom of the list.

My question is how can add them back at a specific index in the list?
(I have a list of 10 items but remove two if they click an option button. If
they click back to the original value of the option group I want to add the
2 values back to the list. But in the right place.)

Any ideas?
 
E

Eliyahu Goldin

Joe,

First, remember the original index for the removed items.

Second, increment the ddl list by 2, as you do.

Third, move all options starting from the original position 2 positions up.
Do it in reverse order, starting from the highest position.

Forth, copy 2 removed item back to the original position.

Eliyahu
 
J

Joe Fallon

Elihayu,
Thanks for the tip.
I thought it might have to be something along those lines.

FYI - what I ended up doing was making a copy of the original list and then
using it to overwrite the modified list if they decided to change back. The
copy is only created once so they can toggle as many times as they want.
Seems to work fine.

Thanks again.
--
Joe Fallon




Eliyahu Goldin said:
Joe,

First, remember the original index for the removed items.

Second, increment the ddl list by 2, as you do.

Third, move all options starting from the original position 2 positions
up.
Do it in reverse order, starting from the highest position.

Forth, copy 2 removed item back to the original position.

Eliyahu

Joe Fallon said:
I can add items to my dropdown list using javascript like this:

cbo.length = j+2;

cbo.options[j].text = "Some Text";

cbo.options[j].value = "MyValue";

cbo.options[j+1].text = "Some Other Text";

cbo.options[j+1].value = "MyOtherValue";

But the above code adds them to the bottom of the list.

My question is how can add them back at a specific index in the list?
(I have a list of 10 items but remove two if they click an option button. If
they click back to the original value of the option group I want to add the
2 values back to the list. But in the right place.)

Any ideas?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top