[jQuery] Next/Previous element in each loop

[jQuery] Next/Previous element in each loop


Hey all, I'm loop over some nodes with each() and I need to look at
the next and previous elements for the current iteration.
<script type="text/javascript">
    $(function() {
        $("div").each(function(i) {
            var prev = [SELECTOR FOR PREVIOUS DIV].text();
            var next = [SELECTOR FOR NEXT DIV].text();
            alert(prev + " : " + next);
        });
    });
</script>
<div>1</div>
<div>2</div>
<div>3</div>
Will I have to store a reference to the divs and access it with i in
the loop like this:
<script type="text/javascript">
    $(function() {
        var divs = $("div");
        divs.each(function(i) {
            var prev = "";
            var next = "";
            if (i > 0)
                prev = $(divs.get(i - 1)).text();
            if (i < divs.size() - 1)
                next = $(divs.get(i + 1)).text();
            alert(prev + " - " + next);
        });
    });
</script>
<div>1</div>
<span>Spanner in the works</span>
<div>2</div>
<span>Don't select me!</span>
<div>3</div>
Is next() the answer maybe?