SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Copy select using JS

    I'm trying to duplicate a select menu using JS. I'm very close to getting it right but need some help. What I have so far:

    Code JavaScript:
      for(i=document.getElementById('original-menu').options.length-1;i>=0;i--)
    	{
    		sel.options[i] = document.getElementById('original-menu').options[i];
    	}

    What it is doing is actually moving the options from select menu A to select menu B and what I want is to duplicate it. Right now when I execute the function that contains the above the original dropdown menu ends up empty with no options.

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,597
    Mentioned
    24 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by warpdesign View Post
    I'm trying to duplicate a select menu using JS. I'm very close to getting it right but need some help.
    The following should create new options copying the value and text from the original options rather than moving the options.

    Code JavaScript:
      for(i=document.getElementById('original-menu').options.length-1;i>=0;i--)
    {
       sel.options[i] = 
         new Option(document.getElementById('original-menu').options[i].value,
                    document.getElementById('original-menu').options[i].text);
    }
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2006
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! I'll give it a try.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •