Languages
[Edit]
EN

JavaScript / XPath - get following sibling element

7 points
Created by:
Wiktor-Sribiew
560

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>

Alternative titles

  1. JavaScript / XPath - get next sibling element
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

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