473,327 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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 1173
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

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

Similar topics

3
by: Buz | last post by:
I am having a problem with extra, unwanted space showing up on my web form tables. I've tried hand coding and can get the htm file to display fine. But as soon as I bring the form into Visual...
1
by: Me | last post by:
I need to test for blank inputs and run a routine "if" the input is blank. This will be used internally on a LAN to calculate selling price if the "sell" input is blank and a margin is supplied,...
3
by: Jeff Calico | last post by:
Hello everyone I am transforming an XML document to text, basically only outputting a small portion of it. When I run the following XSLT via Xalan's processor, I get a bunch of unwanted blank...
5
by: Tom | last post by:
I am having a problem where an extra gap (whitespace, padding) appears in a table cell where I don't want it. The data fed in the table is via dynamic HTML, so the amount in the middle cell may...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.