Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Autosize <Select>

Question posted by: balabaster (Expert) on May 15th, 2008 03:19 PM
I have an array of values that I need to populate a dropdownlist (read <Select>) at runtime. In the name of performance (and because there's dozens of these drop down lists on the page) each Select object is being populated on mouse over.

Given that I have my array already populated, is there an easy way to set the width of the drop down lists given the data in my array? My initial thought was to dump the array contents into a DIV and grab the width, applying it to the style for each of the SELECT tags. Can anyone think of anything more elegant?

Cheers
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
pronerd's Avatar
pronerd
Expert
300 Posts
May 15th, 2008
10:02 PM
#2

Re: Autosize <Select>
Quote:
Originally Posted by balabaster
Can anyone think of anything more elegant?

Yes. You can use the innerHTML attribute of the select tag to create the option tags inside the select tag. Or you can use the DOM JavaScript API to create option elements and attach them as children of the select tag.

This code is untested, so it may have some bugs, but it should be close to what you need.

Code: ( text )
  1. <select id="dynamicDropDown" name="dynamicDropDown" onmouseover="populate(this)" >
  2.     </select>
  3.  
  4.     <script>
  5.         var options = new Array();
  6.  
  7.         options[0] = "one";
  8.         options[1] = "two";
  9.         options[2] = "three";
  10.         options[3] = "four";
  11.  
  12.         function populate(selectTag) {
  13.            
  14.             var optHTML = "";
  15.  
  16.             for( int i = 0; i < options.length; i++ ) {
  17.                 optHTML += " <option value=\""+options[i]+"\" >"+options[i]+"</option>";
  18.             }
  19.  
  20.             selectTag.innerHTML = optHTML;
  21.         }
  22.  
  23.     </script>




or





Code: ( text )
  1. <select id="dynamicDropDown" name="dynamicDropDown" onmouseover="populate(this)" >
  2.     </select>
  3.  
  4.     <script>
  5.         var options = new Array();
  6.  
  7.         options[0] = "one";
  8.         options[1] = "two";
  9.         options[2] = "three";
  10.         options[3] = "four";
  11.  
  12.         function populate(selectTag) {
  13.  
  14.             for( int i = 0; i < options.length; i++ ) {
  15.                 var optTag = document.createElement("option");
  16.                 optTag.setAttribute("value", options[i]);
  17.                 optTag.setAttribute("name", options[i]);
  18.                 selectTag.appendChild(opt);
  19.             }
  20.  
  21.         }
  22.  
  23.     </script>

Reply
rnd me's Avatar
rnd me
Expert
152 Posts
May 15th, 2008
10:30 PM
#3

Re: Autosize <Select>
Quote:
Originally Posted by pronerd
Yes. You can use the innerHTML attribute of the select tag to create the option tags inside the select tag.


ie6 will screw up combos populated that way...

faster and easier:
var tmpOp = new Option("text", "value");

Reply
acoder's Avatar
acoder
Site Moderator
9,896 Posts
May 16th, 2008
09:31 AM
#4

Re: Autosize <Select>
Yet another alternative:
Code: ( text )
  1. var opt = document.createElement('option');
  2. opt.text = "one";
  3. opt.value = "one";
  4. // then add
  5. try {
  6.     selObj.add(y,null); // standards compliant
  7. } catch (exc) {
  8.     selObj.add(y); // IE only
  9.   }

Reply
Reply
Not the answer you were looking for? Post your question . . .
170,098 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top Javascript / DHTML / Ajax Forum Contributors