I have such a code
if ($('#bar #username').text()!=$('.widget-' + widget.id).find('.user').text())
{
//do something
}
Unfortunately, it doesn't go through if $('.widget-' + widget.id).find('.user').text()
does not exist/ underfined.
How to check if it is defined?
I've tried if (typeof $('.widget-' + widget.id).find('.user') === "undefined")
to check whether it is defined or not, but it doesn't help because
alert (typeof $('.widget-' + widget.id).find('.user'))
shows Object
,
alert (typeof $('.widget-' + widget.id).find('.user').text())
shows String
.
As per the jQuery FAQ, check the length
property:
if ($('.widget-' + widget.id).find('.user').length) {
// it exists
}
If you want to see whether a jQuery object matched any DOM elements, use $("whatever").length
-- it will be nonzero.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With