Click here to Skip to main content
15,923,087 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have to iterate all TR, extract the value of id "attrName"/"attrValue"and store the value in array. How to achieve this using JQuery.I need to make the report in word.

HTML
<table class="attrTable" cellpadding="0px" cellspacing="0px">
<tbody>
<tr valign="top"><td class="attrName">Dep Type</td><td class="attrValue">Eng</td></tr>
<tr valign="top"><td class="attrName">Location</td><td class="attrValue">Ban</td></tr><tr valign="top"><td class="attrName">Dep Status</td><td class="attrValue"></td></tr><tr valign="top"><td class="attrName">Office Type</td><td class="attrValue">HQ</td></tr>
<tr valign="top"><td class="attrName">Cont sect</td><td class="attrValue">Sa</td></tr>
<tr valign="top"><td class="attrName"> Strength</td><td class="attrValue"></td>122</tr></tbody></table>


What I have tried:



var table = document.getElementById('attrTable');
var rowLength = table.rows.length;
for (var i = 0; i &lt; rowLength; i += 1) {
var row = table.rows[i];
var cellLength = row.cells.length;
for (var y = 0; y &lt; cellLength; y += 1) {
var cell = row.cells[y];

}
}
Posted
Updated 21-Feb-17 0:27am

1 solution

try this

HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

JavaScript
<script>
        var ary = [];
        $(function () {
            $('.attrTable tr').each(function (a, b) {
                var name = $('.attrName', b).text();
                var value = $('.attrValue', b).text();
                ary.push({ Name: name, Value: value });
               
            });
            alert(JSON.stringify( ary));
        });
    </script>


Demo: JSFiddle [^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900