Solved

How to clone or copy a combobox dinamically ??

Posted on 2008-06-18
13
1,783 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
Comment Utility
Do you have to use document.write("<SELECT ... ?
Why?
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks to all, the first solution given by Zvonko was what i was looking for.
0
 

Author Comment

by:danielc1998
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Closing Comment

by:danielc1998
Comment Utility
thanks.
0
 

Author Comment

by:danielc1998
Comment Utility
thanks
0
 
LVL 19

Expert Comment

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

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
Great tip Zvonko.

0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
:)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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.
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…

762 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

6 Experts available now in Live!

Get 1:1 Help Now