Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Selector to find content of td

Tags:

jquery

My HTML structure looks something like below:

<div id="mainDiv">
<table></table>
<table></table>
<table>
<tr>
<td id="first">FirstCell</td><td id="second">SecondCell</td>
</tr>
<tr>
<td></td><td></td>
</tr>
</table>
</div>

Now I would like the find the content of second td based on the value of first td.

I have tried following but didn't seem to work.

$("#mainDiv tr td:contains('first')").each(function(){
                var secondCell = $(this).find('#second').value();
            });

Any help will be highly appreciated.

like image 367
premsh Avatar asked Oct 13 '25 07:10

premsh


1 Answers

Based on your html structure you can achieve what you want like this

$("#mainDiv tr td:contains('First')").each(function(){
                var secondCell = $(this).next().text();
                console.log(secondCell);
            });​

Working Fiddle

Like @Blender said .value() is not a function, use .text() instead.

like image 123
Prasenjit Kumar Nag Avatar answered Oct 16 '25 06:10

Prasenjit Kumar Nag