0

suppose I have this table:

<table id="mytable" border="1">
<tr id="element">
  <td>value1</td>
  <td>text1</td>
 </tr>
 <tr id="element">
  <td>value2</td>
  <td>text2</td>
 </tr>
</table>

I need to get get the value "text2". What I have so far is this:

var _value = $('#mytable tr:eq(2) td:eq(1)').val();
alert(_value);

My alert says "undefined"

Thank you.

Ibanez1408
  • 4,550
  • 10
  • 59
  • 110

2 Answers2

2

You can use this. Keep in mind ids must be unique so #element should be a class if you're going to use it.

Also there is no "value" assigned to that table cell. So .val() won't work. You can use .text() instead.

You're also trying to use .eq() in the selector when it is it's on method. So you need to choose the selector or your starting point and then chain the .eq() to select the element you need.

var _value = $('#mytable td').eq(3).text();
alert(_value);

Demo

var _value = $('#mytable td').eq(3).text();
alert(_value);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="mytable" border="1">
  <tr id="element">
    <td>value1</td>
    <td>text1</td>
  </tr>
  <tr id="element">
    <td>value2</td>
    <td>text2</td>
  </tr>
</table>
Joe B.
  • 1,124
  • 7
  • 14
0

This question are similar to another that you cant find here

But i think that in your case must to be more similar with something like this:

$('#mytable tr').each(function() {
    var customerId = $(this).find("td").eq(1).html();    
    console.log(_value);
});
Yago Azedias
  • 4,480
  • 3
  • 17
  • 31