suppose I have an
:
if ($("li.test").next().length > 0) { ... }
http://jsfiddle.net/Kfsku/
You need to check the length property.
if ($("li.test").next().length) {
//has next
} else {
//last elem
}
Keep in mind that jQuery usually returns itself so you can chain. Getting the length
property of that object will give you the information
$("li.test").next().length
;
Example
A jQuery selection will always evaluate to boolean true
(as in an if
statement). This is because it is not a native Javascript type -- it is an object.
You need to check the length property of the selection. If it is empty, this will be 0
and will evaluate to false
, so the if
will not pass. If it is not empty, it will be a positive integer and will therefore evaluate to true
, so the conditional will pass.
if ($("li.test").next().length) { …… }