Languages
[Edit]
EN

JavaScript / XPath - get next element to current one

7 points
Created by:
Marcin
16330

In this short article we would like to show how to get next one element to current one using XPath and JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

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

    var result = document.evaluate('following-sibling::*', currentElement);
    var nextElement = result.iterateNext();

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

 

Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - friendly IT community for everyone.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join