943,703 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 526
  • PHP RSS
Nov 3rd, 2008
0

adding Options to Drop Down menu

Expand Post »
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 ??!!!!!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
yasmena is offline Offline
45 posts
since May 2008
Nov 3rd, 2008
0

Re: adding Options to Drop Down menu

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:
javascript Syntax (Toggle Plain Text)
  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.
javascript Syntax (Toggle Plain Text)
  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.
Reputation Points: 93
Solved Threads: 70
Posting Pro
Atli is offline Offline
526 posts
since May 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Is That Possible in PHP
Next Thread in PHP Forum Timeline: echo statement in form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC