Solved

How to clone or copy a combobox dinamically ??

Posted on 2008-06-18
13
1,797 Views
Last Modified: 2013-11-05
I have a loop that creates comboboxes and the fill them in with data, the problem is that the function MakeList2() is call for each combobox created. Considering that aData.length= 30 and aFormula.length=700 , it takes an unacceptable amount of time to build the UI.

Unnecessary parts of the original code have been removed:

for(var i=0;i<aData.length;i++){
   document.write("<SELECT id=selSubs"+j+" class=inlabel onchange=NewChangeToSave()></SELECT>"
 
   // fill in the formulacombos with data
   var elId=document.getElementById("selSubs"+j)
   MakeList2(elId,aFormula,1);
}

function MakeList2(Name,Content,shift){
    if(Content.length>0){
      var i
      var j=0
      for(i=shift;i<Content.length;i=i+2){
          j++
          newOp=document.createElement("OPTION");
          newOp.text=Content[i]
          newOp.value=Content[i+1]
          Name.options[j-shift]=newOp ;
      }
    }
}

How can I copy the content to all the comboboxes without going thought the aFormula and creates all the OPTION elements each time ???

I have tried this , but it does not work:

for(var i=0;i<aData.length;i++){
   document.write("<SELECT id=selSubs"+j+" class=inlabel onchange=NewChangeToSave()></SELECT>"

   // fill in the formulacombos with data
   if(j==0){
       var elId=document.getElementById("selSubs"+j)
       MakeList2(elId,aFormula,1);
   }else{
      var elId0=document.getElementById("selSubs"+(j-1))                
      var elId=document.getElementById("selSubs"+j)
       elId=elId0
  }
}

Thank for your help !!


0
Comment
Question by:danielc1998
  • 5
  • 4
  • 3
13 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 21815763
Do you have to use document.write("<SELECT ... ?
Why?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21815979
How about this:
<body>
<form>
<script>
aFormula = "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z".split(",");
aData = [1,2,3];
var theForm = document.forms[0];
for(var i=0;i<aData.length;i++){
  var newSel = document.createElement("<SELECT id=selSubs"+i+" class=inlabel onchange=NewChangeToSave()>");
  // fill in the formulacombos with data
  MakeList2(newSel,aFormula,0);
  theForm.appendChild(newSel);
}
 
function MakeList2(theSel,Content,shift){
    if(Content.length>0){
      var opt = theSel.options;
      for(var j=shift;j<Content.length;j=j+2){
        var newOpt = new Option(Content[j],Content[j+1]);
        opt[opt.length]=newOpt;
      }
    }
}
 
 
</script>
</form>
</body>

Open in new window

0
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 250 total points
ID: 21816515
Is this what you want??
// create a dropdownlist and append it the body
var ddl = document.createElement("SELECT");
// specify a unique id
// add other attributes like className
ddl.id = "ddl_0";
document.body.appendChild(ddl);
// append 500 options
for(var x=0; x<500; x++)
	ddl.options.add(new Option(x, x));
 
// clone the select element and append it to the body
for(x=0; x<30; x++)
{
	var ddlClone = ddl.cloneNode(true);
	// specify a different id to the dropdownlist
	ddlClone.id = "ddl_" + (x + 1);
	document.body.appendChild(ddlClone);
}

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:danielc1998
ID: 21823334
Thanks to all, the first solution given by Zvonko was what i was looking for.
0
 

Author Comment

by:danielc1998
ID: 21823704
Ups!!!!! I made a mistake, it is AlbertVanHalen code what i needed !!!!!! How can I revert my anwser accepted ??? Sorry .
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21824164
Post a request to Community Support to reopen your question.
Open a question at this topic area: http:/Community_Support/General/
Place in it a link to this question.
0
 

Author Closing Comment

by:danielc1998
ID: 31468512
thanks.
0
 

Author Comment

by:danielc1998
ID: 21848368
thanks
0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 21849310
You're welcome, and thanks for the points...
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21850468
One more note: the accpted solution does NOT produce a valid form.
The select elements are appended to the body node and not to the form node. The select values cannot be submitted by the form because they are outside of the form.
My proposal appended the select element controls to the form.

0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 21850605
Great tip Zvonko.

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 21850685
:)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now