469,612 Members | 1,674 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,612 developers. It's quick & easy.

Unwanted blank elements

Claus Mygind
571 512MB
When I examine the arrays I have created, I see these extra commas (blank elements) inserted before each entry. They are blank elements, but I do not know why the code generates them (see code snippet).

I loop through the options array of the select element in my function and only add elements to the array if they have been selected. So what is happening?

,,,,,,,,,,,,,,,15. Layout,,,,,,,,,,,,,,,,,,,,,,,,,40. Enviromental,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,70. Concrete and Asphalt Lab.
15
"15. Layout"
40
"40. Enviromental"
70
"70. Concrete and Asphalt Lab."

Expand|Select|Wrap|Line Numbers
  1.            <select
  2.             id  ="PHASECODE"
  3.             name="PHASECODE"
  4.             class="InputText"
  5.             disabled="disabled"
  6.             multiple="multiple"
  7.             size="10"
  8.         >
  9.             <option value="10">10. Drilling</option>
  10.             <option value="15">15. Layout</option>
  11.             <option value="30">30. Geotechnical Eng.</option>
  12.             <option value="40">40. Enviromental</option>
  13.             <option value="50">50. Design Services</option>
  14.             <option value="60">60. Construction Material Eng.</option>
  15.             <option value="70">70. Concrete and Asphalt Lab.</option>
  16.             <option value="80">80. Soils Lab.</option>
  17.             <option value="92">92. Maint. Shop Activites</option>
  18.         </select>
  19.         <input
  20.             type="button" 
  21.             id="bAddPhaseCodes" 
  22.             value="Add more Depts. to this template" 
  23.             onclick="addPhase(this);"
  24.         >
  25.  
  26. function addPhase(obj)
  27. {
  28.   cGroupName = document.getElementById("GROUPNAME").value;
  29.   cDept = document.getElementById("JDEPTID").value;
  30.   for (var i = 0; i < document.getElementById("PHASECODE").options.length; i++ )
  31.   {
  32.     if ( document.getElementById("PHASECODE").options[i].selected )
  33.     {
  34.      aPhaseCodes[document.getElementById("PHASECODE").options[i].value] = document.getElementById("PHASECODE").options[i].text; 
  35.    }
  36.   }
  37.  
  38.     //add phase codes to department array
  39.     aDept[cDept] = aPhaseCodes
  40.  
  41.     //add department array to group name (template) array
  42.     aTemplates[cGroupName] = aDept;
  43. alert(aPhaseCodes.length);
  44. }
  45.  
  46.  
Oct 28 '08 #1
3 997
acoder
16,027 Expert Mod 8TB
You're using the value as an index. When you set the index greater than the length of the array, the array automatically increases the size and fills the indices in between with blanks or undefined.
Oct 28 '08 #2
Claus Mygind
571 512MB
Thank you very much.

My workaround to using the number as a label in the array was to add a blank after the value. Will that cause me problems when examining the array?

Expand|Select|Wrap|Line Numbers
  1.  
  2. if ( document.getElementById("PHASECODE").options[i].selected )
  3. {
  4.   cWk = document.getElementById("PHASECODE").options[i].value+' ';
  5.   aPhaseCodes[cWk] = document.getElementById("PHASECODE").options[i].text; 
  6. }
  7.  
Oct 28 '08 #3
acoder
16,027 Expert Mod 8TB
It can because it's using object syntax, but you could just use a for..in loop. One other option is to use an object {}.
Oct 28 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.