Reputation: 3872
I've the below html.
<table border="1" class="myTable">
<tr>
<th class="cname">Component</th>
<th class="pname">Properties</th>
<th class="sname">lqwasb10</th>
<th class="sname">lqwasb11</th>
</tr>
<tr>
<td class="cname">InventoryManager</td>
<td class="pname">maxConcurrentUpdateRetries</td>
<td class="pvalue">1</td>
<td class="pvalue">1</td>
</tr>
<tr>
<td class="cname">CatalogTools</td>
<td class="pname">queryASAFFabrics</td>
<td class="pvalue">skuType="ASAF_FABRIC" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand</td>
<td class="pvalue">skuType="ASAF_FABRIC" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand</td>
</tr>
<tr>
<td class="cname">CatalogTools</td>
<td class="pname">loggingDebug</td>
<td class="pvalue">false</td>
<td class="pvalue">false</td>
</tr>
</table>
Have written the below jquery and it is not working.
$(document).ready(function(){
$('.myTable th').each(function(){
var server = $(this).html();
if(server === 'lqwasb10'){
var b10 = $('.myTable tr td pvalue').text();
alert(b10);
}
});
});
I expected the b10 could contain the below values in order.
The above code doesn't return anything. I'm a jquery newbie, it would be great if someone can help me with a solution.
Many Thanks in advance.
Upvotes: 0
Views: 1127
Reputation: 207861
Assuming that the column you need might not always be the third column, you can use:
var idx;
// Find index of cell with 'lqwasb10'
$('.myTable th').each(function(index) {
if ($(this).text() === 'lqwasb10') idx = index;
})
// Loop through each cell with the same index
$('.myTable tr').each(function() {
console.log($(this).find('td:eq('+idx+')').text())
})
var idx;
// Find index of cell with 'lqwasb10'
$('.myTable th').each(function(index) {
if ($(this).text() === 'lqwasb10') idx = index;
})
// Loop through each cell with the same index
$('.myTable tr').each(function() {
console.log($(this).find('td:eq('+idx+')').text())
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1" class="myTable">
<tr>
<th class="cname">Component</th>
<th class="pname">Properties</th>
<th class="sname">lqwasb10</th>
<th class="sname">lqwasb11</th>
</tr>
<tr>
<td class="cname">InventoryManager</td>
<td class="pname">maxConcurrentUpdateRetries</td>
<td class="pvalue">1</td>
<td class="pvalue">1</td>
</tr>
<tr>
<td class="cname">CatalogTools</td>
<td class="pname">queryASAFFabrics</td>
<td class="pvalue">skuType="ASAF_FABRIC" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand</td>
<td class="pvalue">skuType="ASAF_FABRIC" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand</td>
</tr>
<tr>
<td class="cname">CatalogTools</td>
<td class="pname">loggingDebug</td>
<td class="pvalue">false</td>
<td class="pvalue">false</td>
</tr>
</table>
Note that there's a small typo in your code example in your question. You have an extra <tr>
after the first body row.
Upvotes: 2
Reputation: 42044
In order to print all cells belonging to the third column you can select these cells:
$('.myTable tr:gt(0) td:nth-child(3)')
$('.myTable tr:gt(0) td:nth-child(3)').each(function(){
var b10 = $(this).text();
console.log(b10);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table border="1" class="myTable">
<tr>
<th class="cname">Component</th>
<th class="pname">Properties</th>
<th class="sname">lqwasb10</th>
<th class="sname">lqwasb11</th>
</tr>
<tr>
<td class="cname">InventoryManager</td>
<td class="pname">maxConcurrentUpdateRetries</td>
<td class="pvalue">1</td>
<td class="pvalue">1</td>
</tr>
<tr>
<tr>
<td class="cname">CatalogTools</td>
<td class="pname">queryASAFFabrics</td>
<td class="pvalue">skuType="ASAF_FABRIC" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand</td>
<td class="pvalue">skuType="ASAF_FABRIC" AND NOT basicColor IS NULL ORDER BY dynamicAttributes.fabricpriceband, basicColor, dynamicAttributes.fabrictype, dynamicAttributes.asafpattern, dynamicAttributes.asaffabricbrand</td>
</tr>
<tr>
<td class="cname">CatalogTools</td>
<td class="pname">loggingDebug</td>
<td class="pvalue">false</td>
<td class="pvalue">false</td>
</tr>
</table>
Upvotes: 2