Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the text of nth TD with all tr having particular class

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

like image 842
jerin Avatar asked Aug 24 '13 08:08

jerin


3 Answers

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>
like image 80
chucknelson Avatar answered Nov 08 '22 10:11

chucknelson


Try this

$('tr.warning').each(function(){  
    alert($(this).find('td').eq(1).text());
    });

Demo Here

like image 21
S. S. Rawat Avatar answered Nov 08 '22 09:11

S. S. Rawat


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/

like image 11
Dipesh Parmar Avatar answered Nov 08 '22 09:11

Dipesh Parmar