I have the jquery:
$(“.item”)
which gives me all elements of class item.
I need to select an item from the array I get from this method, and then find the item before this.
something like: $(“.item”).select(“#3”).prev() – except it should work 🙂
so, assuming I had the list of items:
<div id=1 class="item"></div>
<div id=2 class="item"></div>
<div class="somethingElse"></div>
<div id=3 class="item"></div>
<div id=4 class="item"></div>
I should get the “<div id=2></div>” item.
Any ideas?
prevonly looks at the immediate previous sibling. Try this:That should get what you want. As mentioned by googletorp, doing
$('.item').find('#3');is redundant and slower. IDs are supposed to be unique so you should be fine to do$('#3')directly. Do note, however, that IDs, per the spec, are not supposed to start with numbers: