Home>

There are several buttons on the page. There are div and svg inside the buttons.

<button class="wpOab " type="button"><div class="QBdTU B5bH7"><svg class="news" color="#8e8e8e" fill="#8e8e8e" height="24"
...
</svg></div></button>

The classes are randomly generated, one difference is that the svg inside the buttons has class="news". And you need it through document.querySelector(".myclass"); get not the svg object, but specifically its parent button object. How can I do that?

  • Answer # 1
    let newsSvg= document.querySelector('.news');
    let res= newsSvg.closest('button');
    console.log(res);
    <button class="wpOab" type="button">  <div class="QBdTU B5bH7">    <svg class="news">    </svg>  </div></button>