Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why $.isEmptyObject($('#id-does-not-exist-on-page')) returns false?

Tags:

jquery

I'm little confused by jQuery.isEmptyObject method.

$.isEmptyObject([]) -> returns true

but

$('#id-does-not-exist-on-page') => []

$.isEmptyObject($('#id-does-not-exist-on-page')) returns false

why ? thanks.

like image 621
dfang Avatar asked Jun 20 '26 08:06

dfang


1 Answers

From isEmptyObject,

Check to see if an object is empty (contains no properties)

but,

$('#id-does-not-exist-on-page') still has properties.

$('#id-does-not-exist-on-page').addClass('abra_ka_dabra') // valid

[].addClass('abra_ka_dabra'); // TypeError: Object has no method 'addClass'

So,

try this instead

$.isEmptyObject($('#id-does-not-exist-on-page')[0])

like image 199
Jashwant Avatar answered Jun 30 '26 07:06

Jashwant