is there a simple way to get the previous occurrence of an element in the DOM? If I\'m looking at #text3 and I want to get ahold of the previous input #text2<
#text3
#text2<
You can use $.index(), like so:
var previous = null; var inputs = $('input'); var index = inputs.index(this); if (index > 0) { previous = $(inputs[index-1]) }
For example: http://jsfiddle.net/pYaBL/1/