The code which i am working on is as follows:
<table>
<tr class="warning">
<td> 1 </td>
<td> name </td>
<td> address </td>
<td> phone no </td>
<td> Location </td>
</tr>
<tr>
<td> 3 </td>
<td> name2 </td>
<td> address2 </td>
<td> phone no2 </td>
<td> Location2 </td>
</tr>
<tr class="warning">
<td> 6 </td>
<td> name5 </td>
<td> address5 </td>
<td> phone no5 </td>
<td> Location5 </td>
</tr>
<tr>
<td> 7 </td>
<td> name6 </td>
<td> address6 </td>
<td> phone no6 </td>
<td> Location6 </td>
</tr>
I like to get the text of all second TD with all tr with class warning.
I have tried using _.each method but wasn't succesful
Using jQuery's :nth-child
selector along with .each()
should work for you.
Fiddle: http://jsfiddle.net/chucknelson/KDy8X/
Jquery
$(function() {
//use the :nth-child selector to get second element
//iterate with .each()
$('table tr.warning td:nth-child(2)').each(function(index, element) {
var name = $(element).html();
$('#name-list').append('<li>' + name + '</li>');
});
});
Some additional HTML:
<div id="warning-names">
Warning Names:
<ul id="name-list">
</ul>
</div>
Try this
$('tr.warning').each(function(){
alert($(this).find('td').eq(1).text());
});
Demo Here
Try
$('tr.warning td:eq(1)').text();
if you want more specific answer than use :nth-child
selector
$('tr.warning td:nth-child(2)').text()
http://jsfiddle.net/dbuZG/5/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With