Languages
[Edit]
EN

JavaScript / XPath - get following sibling element

7 points
Created by:
AnnLen
8880

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

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

Quic solution:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <div id="element">
    <div>
      <div>User ID</div>
      <div>5f7c2487</div>
    </div>
  </div>
  <script>
    
    var element = document.querySelector('#element');

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

    if (element) {
      console.log(element.innerText);
    }
    
  </script>
</body>
</html>
Native Advertising
50 000 ad impressions - 449$
ūüöÄ
Get your tech brand or product in front of software developers.
For more information contact us:
Red dot
Dirask - friendly IT community for everyone.

‚̧ԳŹūüíĽ ūüôā

Join