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?
- <!DOCTYPE html>
- <html><head>
- <title>jQuery .prevAll() Selector Test</title>
- <script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
- <script type="text/javascript">
- $(function() {
- var dividers = $(".findme").prevAll(".divider:first");
- window.alert(dividers.text());
- });
- </script>
- </head><body>
- <ul>
- <li class="divider"><strong>A Divider</strong></li>
- <li class="findme">Get My Divider</li>
- <li>Some Text</li>
- <li>Some Text</li>
- <li class="divider"><strong>B Divider</strong></li>
- <li>Some Text</li>
- <li>Some Text</li>
- <li>Some Text</li>
- <li class="divider"><strong>C Divider</strong></li>
- <li>Some Text</li>
- <li class="findme">Get My Divider</li>
- <li>Some Text</li>
- </ul>
- </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