Chris
Chris

Reputation: 3

JQuery Multidimensional Array not picking up val in the array

I have a table with ~26 td and they are all checkboxes with a value defined for each one. There could be an infinite number of rows.

Here is an example of what one of the td looks like:

<td style="text-align:center"><input type="checkbox" class="largerCheckbox" id="Missing Date" value="true"'+ZMISSDATE+'></td>

I want to create of an array in JQuery where the array will show the value on each row. I got the array completed but when I console log it, all my td's for each row is coming out blank.

Here is my JQuery to create the array:

        var mainArray = [];
        $("table#dt-multi-checkbox tr ").each(function(){   /* Loop through all rows of the table(id='dt-multi-checkbox') */
            var tempArray = [];                        /* Declare a temporary array */
            $(this).find('td').each(function(){        /* Loop through all elements inside a row */
                tempArray.push($(this).val());        /* Use array push() function to add an element to the array */
            });
            mainArray.push(tempArray);
        });

        console.log(mainArray);

Can somebody tell me where I am going wrong? Why is the value ="true not showing in my array (from the example td I mentioned above) and how can I make it show the value in the array? Here is an example of what it looks like when I console log it:

Array(26)
0: ""
1: ""
2: ""
3: ""
4: ""
5: ""
6: ""
7: ""
8: ""
9: ""
10: ""
11: ""
12: ""
13: ""
14: ""
15: ""
16: ""
17: ""
18: ""
19: ""
20: ""
21: ""
22: ""
23: ""
24: ""
25: ""

Thank you and I appreciate any direction you can guide me.

Upvotes: 0

Views: 47

Answers (1)

fraggley
fraggley

Reputation: 1285

Change your jquery function to:

var mainArray = [];
    $("table#dt-multi-checkbox tr ").each(function(){   /* Loop through all rows of the table(id='dt-multi-checkbox') */
        var tempArray = [];                        /* Declare a temporary array */
        $(this).find("input[type='checkbox']").each(function(){        /* Loop through all elements inside a row */
            tempArray.push($(this).val());        /* Use array push() function to add an element to the array */
        });
        mainArray.push(tempArray);
    });

    console.log(mainArray);

Upvotes: 1

Related Questions