{"id":446,"date":"2022-08-30T15:01:29","date_gmt":"2022-08-30T15:01:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/jquery-existing-of-document-parent-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:29","modified_gmt":"2022-08-30T15:01:29","slug":"jquery-existing-of-document-parent-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/jquery-existing-of-document-parent-collection-of-common-programming-errors\/","title":{"rendered":"jQuery &#8211; existing of document parent-Collection of common programming errors"},"content":{"rendered":"<p>Found a few similar articles, but can&#8217;t seem to get anything to work.<\/p>\n<p>We have this code as follows:<\/p>\n<pre><code>parent.$(\"#toTop\").trigger(\"click\");\n<\/code><\/pre>\n<p>However, we need to check for the existence of the parent element as it is throwing an error in some cases where dom setup is different:<\/p>\n<pre><code>parent.$(...) is null\n<\/code><\/pre>\n<p>How would I do that?<\/p>\n<ol>\n<li>\n<p>Try something like<\/p>\n<pre><code>if(parent.$(\"#toTop\")){\/\/Not true if is undefined \/ null \n\/\/codes\n}\n<\/code><\/pre>\n<p>It is probably safer to use<\/p>\n<pre><code>if(parent.$(\"#toTop\").length)){\n    \/\/codes\n    }\n<\/code><\/pre>\n<\/li>\n<li>\n<pre><code>if( parent &amp;&amp; parent.$(\"#toTop\")) {\n    parent.$(\"#toTop\").trigger(\"click\");\n}\n<\/code><\/pre>\n<p>If you are specifically looking only for the parent you can use this<\/p>\n<pre><code>if( parent )\n<\/code><\/pre>\n<p>There might be a case when the parent gives you a <code>empty<\/code> selector. So you can check for the length in such cases..<\/p>\n<pre><code>if(parent.length) \n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:43:08. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Found a few similar articles, but can&#8217;t seem to get anything to work. We have this code as follows: parent.$(&#8220;#toTop&#8221;).trigger(&#8220;click&#8221;); However, we need to check for the existence of the parent element as it is throwing an error in some cases where dom setup is different: parent.$(&#8230;) is null How would I do that? Try [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-446","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=446"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/446\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}