Jake M Jake M - 6 months ago 32
jQuery Question

How to check if .prev() fails

How can I check if

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


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 )