duykhoa
duykhoa

Reputation: 2302

How to get the data in this example

Given the code like this

<table>
  <tr>
    <td>1</td>
    <td>e</td>
    <td>a</td>
  </tr>
  <tr>
    <td>2</td>
    <td>e</td>
    <td>c</td>
  </tr>
   ...
</table>

The first cell of every row is the id (it's unique) If the input is value of the id, how can I get the value of the third cell in this row

Example: input 1 -> output a
         input 2 -> output c

How can I do it with Jquery?

Upvotes: 1

Views: 72

Answers (5)

user1776226
user1776226

Reputation:

this exactly what you want.

you can use Demo Link

And function look like

$(document).ready(function() {

$('table tr').each(function() {        

  var str = "input "+ $(this).find('td:first').text() + " -> output " + $(this).find('td:last').text();        
    $("#result").append("</br>"+str);
});

});

Upvotes: 1

Jaya Mayu
Jaya Mayu

Reputation: 17257

update

You can do something as below

var valueSearch = "4";
$('tr').each(function(index){
        if($(this).find('td:first').text()==valueSearch){
            alert($(this).find('td:last').text());
        }

 });

Here is a working example at live fiddle

old answer

$('tr').eq("1").find('td:last').text();

Is the correct way of doing it.

Here is a live example on Fiddle

Upvotes: 3

Peter Tadros
Peter Tadros

Reputation: 9297

Using JavaScript, you can use this

var id = document.getElementsByTagName('tr')[0].getElementsByTagName('td')[0].innerHTML;
var value = document.getElementsByTagName('tr')[id].getElementsByTagName('td')[2].innerHTML;

Upvotes: 1

GautamD31
GautamD31

Reputation: 28753

Try like

$('table tr').each(function(){
     alert("input "+ $(this + 'td').eq(0).text() + " -> " + $(this + 'td').eq(2).text());
});

or try like

$('table tr').each(function(){
     alert("input "+ $(this + 'td:first').text() + " -> " + $(this + 'td:last').text());
});

Upvotes: 1

Adil
Adil

Reputation: 148150

You can use td:last

Live Demo

$('tr').eq(index).find('td:last').text();

Upvotes: 5

Related Questions