Jake M Jake M - 1 year ago 62
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

Answer Source

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 )