Preethi
Preethi

Reputation: 45

How to get the cell value by using the row and column positions using jquery

I have created a html table.Now using the row index and column index, I should get the cell value. how to do it Html:

<table border="2" width="200">
<tr>
<td></td>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>1</td>
<td>3</td>
<td>5</td>
<td>5</td>
</tr>
<tr>
<td>2</td>
<td>4</td>
<td>6</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>5</td>
<td>2</td>
<td>4</td>
</tr>
</table>

jQuery:

var a=2;//row index
var b=3;//column index
var c=$("#tab").find('tr:eq(a)').find('td:eq(b)');
alert(c);

And I want to get the cell value using the index. But this code is not working

Upvotes: 1

Views: 4192

Answers (2)

Jai
Jai

Reputation: 74738

change to this with .eq() instead:

var c=$("#tab").find('tr').eq(a).find('td').eq(b).text();
console.log(c); // 2

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67207

Try to concatenate the variables properly,

var a=2;//row index
var b=3;//column index
var c=$("#tab").find('tr:eq('+ a + ')').find('td:eq(' + b + ')');
alert(c.text());

Also you have to use .text() to extract the text content of the particular element.

Your code can also be written as,

var a=2;
var b=3;
var c= $("#tab tr:eq(" + a + ") td:eq(" + b + ")").text();
alert(c);

Upvotes: 4

Related Questions