repopulating a select with a different number of e

Results 1 to 3 of 3

Thread: repopulating a select with a different number of e

  1. #1
    Bill M Guest

    Default repopulating a select with a different number of e

    I have javascript code to repopulate a select box with values from arrays.<BR>If I repopulate the box with a smaller number of elements than the box was originally defined with, I get "undefined" for the last few elements.<BR>How can I resize the list to eliminate the undefineds?<BR><BR>Thanks<BR><BR>Code:<BR>&#060;sc ript language = "JavaScript"&#062;<BR><BR>&#060;!--<BR><BR>var Val = new Array("Please Select a color ","Dark Blue","Green","Red","Burgundy","Black","Grey");<BR >var HD = new Array("Please Select a color","Dark Blue","Green","Red","Burgundy","Black","Grey","Whi te","Medium Blue (Heavy-Duty only)","Ivory (Heavy-Duty only)");<BR><BR><BR>function swapOptions(the_array_name)<BR>{<BR> var the_array = eval(the_array_name);<BR> <BR><BR> setOptionText(window.document.form1.colorselect1, the_array);<BR>}<BR> <BR>function setOptionText(the_select, the_array)<BR> <BR>{<BR> for (loop=0; loop &#060; the_select.options.length; loop++)<BR> {<BR> the_select.options[loop].text = the_array[loop];<BR> }<BR>}<BR><BR> <BR>//--&#062;<BR>&#060;/script&#062;

  2. #2
    Steve Cimino Guest

    Default RE: repopulating a select with a different number

    You have a few things going on in here that I&#039m not quite sure about, but you can simply populate the box based on the length of the ARRAY, not by pre-defining the list box:<BR><BR>for(i=0;i&#060;=myarray.length;i++)<BR >{<BR>//add each element to the listbox dynamically now<BR>}<BR><BR>This way, the list box will never be longer than the array, and you&#039ll have no nasty undefined elements.

  3. #3
    Join Date
    Dec 1969

    Default just assign null to unused options...

    see and the "Dual Lists in HTML" demo for example code. (The values don&#039t come from arrays--they come from another &#060;SELECT&#062;--but the principles are exactly the same.)<BR><BR>Yes, that code works in both MSIE and NS.<BR>

Posting Permissions

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