Search code examples
jquerynextuntil

jQuery: Find nextUntil() any heading element


I am trying to find all the contents until the next heading title. Heading title could be h2, h3, h4, h5 or even h6. That said, in between main and the heading it could have many other elements or a single element so counting them until the next heading is not an option.

How could I achieve this?

This is my current method of which finds up ONLY until h4

$("main").parent().nextUntil("h4").each(function(){
   console.log($(this).html())               
})

Solution

  • Use multiple-selecters in nextUntil() like,

    $("main").parent().nextUntil("h2,h3,h4,h5,h6").each(function(){
       console.log($(this).html())               
    })