Reputation: 45
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
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
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