0

document.querySelector("h2") returns only the first element that matches that class selector.

Could this be done in CSS? It is not like doing: h2:first-child as this would select every <h2> that is the first element of its parent...

Take for example this:

<body>
    <h1>Title</h1>
    <h2>Sub</h2>
    <article>
        <h1>Title2</h1>
        <p>Blabla</p>
        <h2>Introduction</h2>
        <p>Blabla</p>
        <h2>Methods</h2>
        <h2>Conclusions</h2>
    </article>
    <footer>
        <h2>Bla</h2>
    </footer>
</body>

I know there are ways to select only that <h2> containing "Sub", but could I ask like in querySelector: get all of them and filter the first, the second or the third one...?

Vandervals
  • 5,774
  • 6
  • 48
  • 94

0 Answers0