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:

<tr class="warning">
    <td> 1 </td>
    <td> name </td>
    <td> address </td>
    <td> phone no </td>
    <td> Location </td>
    <td> 3 </td>
    <td> name2 </td>
    <td> address2 </td>
    <td> phone no2 </td>
    <td> Location2 </td>
<tr class="warning">
    <td> 6 </td>
    <td> name5 </td>
    <td> address5 </td>
    <td> phone no5 </td>
    <td> Location5 </td>
    <td> 7 </td>
    <td> name6 </td>
    <td> address6 </td>
    <td> phone no6 </td>
    <td> Location6 </td>

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


3 Answers

Using jQuery's :nth-child selector along with .each() should work for you.

Fiddle: http://jsfiddle.net/chucknelson/KDy8X/


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


Try this


Demo Here

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

S. S. Rawat


$('tr.warning td:eq(1)').text();

if you want more specific answer than use :nth-child selector

$('tr.warning td:nth-child(2)').text()


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

Dipesh Parmar