I have a jquery I'm running to get the first < a > tag text, but it seems its getting all of the elements and I'm not sure what I'm doing wrong.
<tr>
<td>
<a>text 1</a>
</td>
<td>
<a>text 2</a>
</td>
<td>
<a>text 3</a>
</td>
<td>
<a>text 4</a>
</td>
<button>click</button>
</tr>
$(button).on('click',function(){
var getText = $(this).closest('tr').next().find('a:first-child');
var getText = $(this).closest('tr').next().find('a:nth-child(1)');//tried this too
})
So the result should be "text 1" for the variable. Right now its returning all < a > tags.
All the a
tags are the first child of it's parent, so get the a
tag within the first td
element. And there is no need to use next()
method in case you want to get the a
tag within the same tr
element.
var getText = $(this).closest('tr').find('td:first-child a');
FYI : Your markup is not valid since you can't use button
tag as the child within the tr
tag, the only allowed tags are td
and th
. So move the button
tag to any of the td
or add a new td
element.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments