JavaScript / XPath - find div that contains specific class

3 points
Created by:

Quick solution, XPath:

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

Practical example:

// ONLINE-RUNNER:browser;

<!doctype html>
  <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>
    var container = document.querySelector('#container');
    var result = document.evaluate("//div[contains(@class, 'city')]", container);

    var element = null;
    while (element = result.iterateNext()) {

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.

❤️💻 🙂