0

i need to add an option from list1 to list2 when the user clicks on ADD to List button
but sth is wrong with my code it's not working

function AddItem()
{



var opt = document.getElementById("List1").selectedIndex;



document.getElementById("List2").options.add(opt);


}

can anyone tell me whats wrong ??!!!!!!

Edited by happygeek: fixed formatting

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Atli
0

The document.getElementById("List1").selectedIndex property would not return the actual element, only the position of the selected option in the options array.

To move it over you would have to do something more like:

var firstBox = document.getElementById('List1');
var secondBox = document.getElementById('List2');

var option = firstBox.options[firstBox.selectedIndex];
secondBox.options.add(option);

That would move the selected option from List1 to List2.

If you wanted to copy it over, you could use the cloneNode method.

var firstBox = document.getElementById('List1');
var secondBox = document.getElementById('List2');

var option = firstBox.options[firstBox.selectedIndex];
var newOption = option.cloneNode(true);
secondBox.options.add(newOption );
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.