Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a cell value from a row based on another cell value

enter image description here

i want to get the age of a particular name ,lets say i want to get the age of Garrett Winters , using jquery . the record can be at any row of the table.i have to search the whole table and get the corresponding age in a variable..

i want to search the column Name for a particular value and get the corresponding age

<table id="table1" border="1" cellspacing="0" width="100%">
        <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Status</th>
            </tr>
        </thead>

        <tfoot>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Status</th>
            </tr>
        </tfoot>

        <tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>CNF</td>
            </tr>
            <tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>CNF</td>
            </tr>
            <tr>
                <td>Ashton Cox</td>
                <td>Junior Technical Author</td>
                <td>San Francisco</td>
                <td>66</td>
                <td>2009/01/12</td>
                <td>CNF</td>
            </tr>
            <tr>
                <td>Cedric Kelly</td>
                <td>Senior Javascript Developer</td>
                <td>Edinburgh</td>
                <td>22</td>
                <td>2012/03/29</td>
                <td>TMP</td>
            </tr>
            <tr>
                <td>Airi Satou</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>33</td>
                <td>2008/11/28</td>
                <td>CNF</td>
            </tr>
            <tr>
                <td>Brielle Williamson</td>
                <td>Integration Specialist</td>
                <td>New York</td>
                <td>61</td>
                <td>2012/12/02</td>
                <td>TMP</td>
            </tr>

        </tbody>
    </table>

i m new to jquery .Help me out

like image 598
Bhupathy A P Avatar asked Aug 18 '15 06:08

Bhupathy A P


2 Answers

You can do something like this. It works for me. Demo

$(document).ready(function(){
   var nameToSearch ="Tiger Nixon";
   $('table tr').each(function(){
        if($(this).find('td').eq(0).text() == nameToSearch)
            alert("Age of "+nameToSearch+" is "+$(this).find('td').eq(3).text());         
   });
});

I hope it helps you.

like image 83
Sapikelio Avatar answered Sep 23 '22 20:09

Sapikelio


Use :contains Psudeo selector in jquery. Get the age of the 'Garrett Winters'

var serachName = 'Garrett Winters';
$("table tbody tr td:contains("+serachName+")").parent().find('td:eq(3)').text()

Fiddle

like image 41
Sudharsan S Avatar answered Sep 19 '22 20:09

Sudharsan S