Sunday, April 28, 2013

If element doesn't not exist

var obj=$("divElement");

If there is no element named "divElement", JQuery will not raise any error and JQuery will do nothing.
If you want to check if the element exists in the document, check the length property:

if ($("divElement").length>0)
{
//element exists, code
}

"length" property will return 0 if the element doesn't  exist.

No comments:

Post a Comment