adding Options to Drop Down menu

Reply

Join Date: May 2008
Posts: 43
Reputation: yasmena is an unknown quantity at this point 
Solved Threads: 0
yasmena yasmena is offline Offline
Light Poster

adding Options to Drop Down menu

 
0
  #1
Nov 3rd, 2008
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 ??!!!!!!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 455
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training

Re: adding Options to Drop Down menu

 
0
  #2
Nov 3rd, 2008
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:
  1. var firstBox = document.getElementById('List1');
  2. var secondBox = document.getElementById('List2');
  3.  
  4. var option = firstBox.options[firstBox.selectedIndex];
  5. 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.
  1. var firstBox = document.getElementById('List1');
  2. var secondBox = document.getElementById('List2');
  3.  
  4. var option = firstBox.options[firstBox.selectedIndex];
  5. var newOption = option.cloneNode(true);
  6. secondBox.options.add(newOption );
Last edited by Atli; Nov 3rd, 2008 at 7:56 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum


Views: 459 | Replies: 1
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC