Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing variable to empty jquery object in if condition [duplicate]

In the jQuery accordion API, it says "If the accordion is collapsing, ui.newHeader and ui.newPanel will be empty jQuery objects."

How can I check if ui.newheader is an empty jQuery object? I've tried it like this:

if ($(ui.newHeader) == null)
{
    ...
}

,like this:

if (ui.newHeader == null)
{
    ...
}

and this:

if ($(ui.newHeader) == "")
{
    ...
}

So basically, this is a question about jquery/javascript syntax :) Thanks

like image 552
taevanbat Avatar asked Jun 29 '13 12:06

taevanbat


2 Answers

What you want is to know if there is 0 element in the set. Do it like this :

if ($(ui.newHeader).length==0) {
like image 71
Denys Séguret Avatar answered Oct 30 '22 14:10

Denys Séguret


if (!$(ui.newHeader).length)

or

if (!$(ui.newHeader)[0])
like image 24
A. Wolff Avatar answered Oct 30 '22 14:10

A. Wolff