Jake M Jake M - 3 months ago 15
jQuery Question

How to check if .prev() fails

How can I check if

.prev()
fails, ie, if the element has no previous sibling?

Aparently on failure it returns an empty jquery object but each of the following checks for failure return false:

alert( ele.prev() === undefined ); // 'false'
alert( $.isEmptyObject( ele.prev() ) ); // 'false'
alert( ele.prev() == $() ); // 'false'


Note ele does indeed not have any previous siblings

Answer

The prev() methods return a jQuery object and you can check it contains any element by checking it's length property.

alert( ele.prev().length == 0 )