1

I have a bunch of user input to store from a javascript from. I like to loop through some counter as it stores the input instead of assigning them one at a time. Is this the correct syntax to specify the element ID? It's not working for me at this point and complains for example catMaxInput: [Exception: ReferenceError: catMaxInput is not defined]

var catInput = [], catMaxInput = [], catCostSFHR = [],  catOccHRStart=[], catOccHREnd =[];
        var z = 1;
        for (var i=0; i<3; i++){
            catMaxInput[i] = (Math.round(parseFloat(document.getElementById("cat\"" + z + "\"Max").value))); 
            z++;
        }

        for (var c=0; c<3; c++){
            catTotalArea[c] = (Math.round(parseFloat(document.getElementById("cat\"" + z + "\"TotalArea").value)));
            z++;
        }

So far this is the form:

<tr>
          <td>CAT 1</td>
          <td><input name="data1Max4" type="text" id="cat1Max" value="20" /></td>
          <td><input name="data1Max7" type="text" id="cat1TotalArea" value="50,000 SF" /></td>
          <td><input name="data1Max10" type="text" id="cat1CostSFHR" value="$ 100.00" /></td>
          <td><input name="data1Max13" type="text" id="cat1OccHRStart" value="6:00am" /></td>
          <td><input name="data1Max16" type="text" id="cat1OccHREnd" value="12:00pm" /></td>
        </tr>
        <tr>
          <td>CAT B</td>
          <td><input name="data2Max5" type="text" id="cat2Max" value="70" /></td>
          <td><input name="data2Max8" type="text" id="cat2TotalArea" value="20,000 SF" /></td>
          <td><input name="data2Max11" type="text" id="cat2CostSFHR" value="$ 50.00" /></td>
          <td><input name="data2Max14" type="text" id="cat2OccHRStart" value="12:00pm" /></td>
          <td><input name="data2Max17" type="text" id="cat2OccHREnd" value="8:00pm" /></td>
        </tr>
        <tr>
          <td>CAT C</td>
          <td><input name="data3Max6" type="text" id="cat3Max" value="100" /></td>
          <td><input name="data3Max9" type="text" id="cat3TotalArea" value="30,000 SF" /></td>
          <td><input name="data3Max12" type="text" id="cat3CostSFHR" value="$ 0.00" /></td>
          <td><input name="data3Max15" type="text" id="cat3OccHRStart" value="8:00pm" /></td>
          <td><input name="data3Max18" type="text" id="cat3OccHREnd" value="6:00am" /></td>
        </tr>
FongYu
  • 693
  • 3
  • 14
  • 23

2 Answers2

4

The result of "cat\"" + i +"\"Max" is a string that has the value of i between ".

You proabably need this to be "cat" + i +"Max".

You are escaping and adding double quotes where you don't need to.

var catInput = [], catMaxInput = [], catCostSFHR = [],  
                                             catOccHRStart=[], catOccHREnd =[];

for (var i=0; i<3; i++){
  catMaxInput[i] = (Math.round(parseFloat(
                            document.getElementById("cat" + i +"Max").value))); 
}

for (var c=0; c<3; c++){
  catTotalArea[c] = (Math.round(parseFloat(
                      document.getElementById("cat" + c+  "TotalArea").value)));
}
Oded
  • 463,167
  • 92
  • 837
  • 979
3

Replace:

getElementById("cat\"" + i +"\"Max")
getElementById("cat\"" + c+  "\"TotalArea")

with:

getElementById("cat"+i+"Max")
getElementById("cat"+c+"TotalArea")

Update: In ES6 you can do:

getElementById(`cat${i}Max`)
getElementById(`cat${c}TotalArea`)

There are better ways of doing this like:

How do I iterate through table rows and cells in javascript?

Yuval Pruss
  • 5,705
  • 11
  • 32
  • 59
iambriansreed
  • 20,961
  • 6
  • 58
  • 73