I am using ParcelJS V2. I have the following code that gets data from an HTML table. Every <td>
in the cell consists of an <input>
tag and the getElementById
refers to the ID of the input tag.
getCount.js
JavaScript
x
28
28
1
let rowCount = document.getElementById("left-col__table").rows.length;
2
3
let dataExport = []
4
let tuple = []
5
6
let i
7
8
function collectData() {
9
10
for (i = 0; i < rowCount - 2; i++) {
11
12
console.log("RowCount: " + rowCount)
13
14
tuple.push(document.getElementById("x-in-" + i.toString()).value)
15
tuple.push(document.getElementById("y-in-" + i.toString()).value)
16
17
console.log(tuple);
18
19
dataExport.push(tuple)
20
21
tuple = []
22
}
23
24
console.log("DataExport:" + dataExport.toString())
25
}
26
27
export default collectData
28
script.js
JavaScript
1
4
1
import collectData from '....'
2
3
collectData()
4
When I check my console, there isn’t log from the collectData
function.
This FOR
loop works fine when I use it in my script.js
but doesn’t work when I export and then import into script.js
.
Are there any ways to export loops using ParcelJS?
Advertisement
Answer
Move
JavaScript
1
2
1
let rowCount = document.getElementById("left-col__table").rows.length;
2
inside your function collectData() {
.
At the time of bundling the DOM is unavailable, rendering your rowCount
undefined
.
When your function is being executed, your loop end condition checks
JavaScript
1
2
1
0 < undefined - 2
2
undefined - 2
results in NaN
.
JavaScript
1
2
1
0 < NaN
2
is false
, so your loop never runs.