Using :first Selector with .prevAll() and Multiple Elements

Using :first Selector with .prevAll() and Multiple Elements

So I've Googled the .prev(".class") issue and found the solution, .prevAll(".class:first"). However, that doesn't seem to work with multiple matched elements. The :first selector seems to be applied to the entire list at the end, what I would expect .prevAll(".class").first() to do. What I would rather have happen is the selector be applied to each element in the "result set".


Does this seem to be a bug or rather just how jQuery works?


  1. <!DOCTYPE html>
  2. <html><head>
  3.       <title>jQuery .prevAll() Selector Test</title>
  4.       <script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
  5.       <script type="text/javascript">
  6.             $(function() {
  7.                   var dividers = $(".findme").prevAll(".divider:first");
  8.                   window.alert(dividers.text());
  9.             });
  10.       </script>
  11. </head><body>
  12.       <ul>
  13.             <li class="divider"><strong>A Divider</strong></li>
  14.             <li class="findme">Get My Divider</li>
  15.             <li>Some Text</li>
  16.             <li>Some Text</li>
  17.             <li class="divider"><strong>B Divider</strong></li>
  18.             <li>Some Text</li>
  19.             <li>Some Text</li>
  20.             <li>Some Text</li>
  21.             <li class="divider"><strong>C Divider</strong></li>
  22.             <li>Some Text</li>
  23.             <li class="findme">Get My Divider</li>
  24.             <li>Some Text</li>
  25.       </ul>
  26. </body></html>

What happens is that the alert message will only have "A Divider" when I would expect it to have "A DividerC Divider". Thoughts? Thanks