I’m fairly new to JQuery and am having trouble with something that I’m sure is simple. Here is an example of some html that I’m working with:
<tr>
<td><div class="testclass"><p>Test text</p></div></td>
<td><button class="testButton">Button text</button></td>
</tr>
When the button is clicked, I want to get the value in <p> in a previous cell. Here’s how I’m currently trying to select it:
var text = $(".testButton").parent().parent().closest('p').text();
Within the onClick function of the button, I’ve also tried:
$(this).prev('p').text();
However, it’s always null. I’ve messed around with jsfiddle for a little while trying to get this right, but no luck. Any tips would be appreciated.
try use:
demo at: http://jsfiddle.net/ZsLZ2/