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 ??!!!!!!

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 );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.