Jquery find closest previous brother that doesn't have class

I'm going astray trying to get the closest previous element with a class β€œslide” that DOES NOT contain a subdivision of a class using jQuery

HTML example:

<section id="p1" class="slide"></section> <section id="p2" class="slide"></section> <section id="p3" class="slide"></section> <!--I AM TRYING TO GET THIS ELEM --> <section id="p31" class="slide subsection"></section> <section id="p32" class="slide subsection"></section> <!--I AM HERE --> <section id="p4" class="slide"></section> 

I tried: (where $ subsection is the above item)

 var $slide = $subsection.prev('.slide').not('.subsection'); var $slide = $subsection.prev('.slide:not(".subsection")'); var $slide = $subsection.prev('.slide').not("[class='subsection']"); var $slide = $subsection.prev('.slide:not([class="subsection"])'); 

Now I found out that prev () selects only one previous item and then stops, but does prevUntil not work either?

 var $slide = $subsection.prevUntil('.slide').not('.subsection'); 

I also tried:

 var $slide = $subsection.prevAll('.slide').not('.subsection'); 

but this gives me the very first element of the slide with id p1.

If anyone has any clues, I will be grateful ... Can someone stop me to escape? 8- |

+6
source share
2 answers

Try the following:

 var a = $("#p32").prevAll(".slide").not(".subsection").first(); alert($(a).text()); 

The spell is here.

+8
source

You can try this

 $('.subsection').prev('.slide:not(".subsection")'); 

An example is here.

+2
source

Source: https://habr.com/ru/post/957170/


All Articles