Languages
[Edit]
EN

JavaScript / XPath - find div that contains specific class

3 points
Created by:
Imaan-Morin
1009

Quick solution, XPath:

//div[contains(@class, 'my-class-name')]

Practical example:

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <div id="container">
      <div class="city">San Francisco</div>
      <div class="city">San Francisco</div>
      <div class="city">New York</div>
      <div class="city">San Francisco</div>
      <div class="city">Los Angeles</div>
      <div class="city">New York</div>
  </div>
  <script>
    
    var container = document.querySelector('#container');
    
    var result = document.evaluate("//div[contains(@class, 'city')]", container);

    var element = null;
    while (element = result.iterateNext()) {
        console.log(element.textContent);
    }
    
  </script>
</body>
</html>

We should see in console all 6 cities:

San Francisco
San Francisco
New York
San Francisco
Los Angeles
New York

 

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