<div id="con" >
<p class="bb">1</p>
<p class="aa">2</p>
<p class="aa">3</p>
<p class="bb">4</p> /*----- i want this reference */
<p class="aa">5</p>
<p class="aa">6</p>
<p id="last" class="aa">7</p>
<p class="aa">6</p>
<p class="aa">6</p>
</div>
i want to find the previous element among the siblings with sorted value.
here i want to find the ‘p’ has class ‘aa’ previous to #last
$("#last").prev('p.bb').text()
// empty string
$("#last").prev().text()
//will give 6
i want 4.. just the text of previous closest ‘p.bb’
I’m unsure of what, precisely, you’re looking for, but assuming you want to find the last
pof classbbthat comes before your#lastelement:JS Fiddle demo.
While this is rather more verbose than @voigtan‘s answer it allows you to use any arbitrary element, and ensures that the
.bbelement found always occurs prior to that element.