Languages
[Edit]
EN

JavaScript / XPath - get following sibling element

7 points
Created by:
AnnLen
13450

In this short article we would like to show how to find following sibling element reference using XPath in JavaScript.

In below example we are looking for following slibling (/following-sibling::div) that occurs as next of any div element that contains User ID text inside (//div[text()='User ID']).

Quic solution:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <div>
    <div>User ID</div>
    <div>5f7c2487</div>
  </div>
  <script>

    var result = document.evaluate("//div[text()='User ID']/following-sibling::div", document.body);
    var followingElement = result.iterateNext();

    if (followingElement) {
      console.log(followingElement.innerText);
    }
    
  </script>
</body>
</html>

JavaScript / XPath

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join