By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,742 Members | 2,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,742 IT Pros & Developers. It's quick & easy.

Unwanted blank elements

Claus Mygind
100+
P: 571
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
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
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
100+
P: 571
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
Expert Mod 15k+
P: 16,027
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.