Solved

How to clone or copy a combobox dinamically ??

Posted on 2008-06-18
13
1,793 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

863 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

19 Experts available now in Live!

Get 1:1 Help Now