Getting next element text

Keith

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.

Pranav C Balan

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.

edited at
0

Comments

0 comments
Login to comment

Related

Getting next sibling element using XPath and Selenium for Java

Getting next element while cycling through a list

Getting the text of a dynamic element in JS

Why is the text getting appended to the wrong html element?

SVG Text element not getting added in IE

Getting text around a specific element reference

Inserting Text Next to SVG element

Getting the index of the next element in a very large memmap which satisfies a condition

Selenium not getting the text of an element

Getting next element in a table javascript

Using Xpath find element by text and print the next element text in python

Getting decimals from text element

Getting the text in a nested element in XML with C#

Getting the text of a paragraph element using Selenium

zclip copy next text present in element

Get text next to the selected element using BeautifulSoup

Get text next to selected element in lxml / Python

Assign JTextField's text to the next element of an array

Dictionary - Getting the next element of a known element

Getting text from first sibling of given element

Getting focus on next input element anywhere on the page

Trouble getting text after an element using xPath

getting the next element with xpath

Getting text outside element but next to Elements using jsoup

Getting the next element in a pandas dataframe

XPath get next element using contains text()?

Python, Selenium: How to get text next to element

How does one find element by text and then copy the next sibling element?

Getting value from rich text element in Contentful?