{"id":621,"date":"2012-05-29T04:36:35","date_gmt":"2012-05-29T04:36:35","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=621"},"modified":"2012-05-29T04:36:35","modified_gmt":"2012-05-29T04:36:35","slug":"jquery-check-for-element-exists","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-check-for-element-exists\/","title":{"rendered":"jQuery Check for Element Exists Examples"},"content":{"rendered":"<p>When using jQuery did you ever wonder how to check for element\u00a0existence\u00a0using a selector? In this article, I am going to show you how you can <strong><em>check if element exists or not on a page, using jQuery<\/em><\/strong>. You can use a selector or a class to find out if that related element exists or not. I am also going to show the right way and the wrong way for checking the element&#8217;s existence, such as a div. You can do all this with just 1 line of code. Read on to find out more.<!--more--><\/p>\n<h2>How to Check for an Element to see if it Exists or Not using jQuery &#8211; Examples<\/h2>\n<p>Now there are two ways of checking if an element exists or not. One is a wrong way and the other is a correct way. Let me show you the wrong way to do this first so that you can avoid doing it in future:<\/p>\n<p><strong>Wrong Way:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">if ($(\"#my_div\"))\n{\n\/\/Do your processing here\n}<\/pre>\n<p><strong>Correct Way:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">if ($(\"#my_div\").length &gt; 0){\n{\n\/\/Do your processing here\n}<\/pre>\n<p>As you can see, <strong>jquery length<\/strong> function is very handy to<strong> check for element exists<\/strong>.<\/p>\n<h5>Your Turn!<\/h5>\n<p>Do you know of any other way to <em><strong>check if element exists or not using jQuery<\/strong><\/em>? Feel free to comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When using jQuery did you ever wonder how to check for element\u00a0existence\u00a0using a selector? In this article, I am going to show you how you can check if element exists or not on a page, using jQuery. You can use a selector or a class to find out if that related element exists or not. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,13],"tags":[41],"class_list":["post-621","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-a1","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/621","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/comments?post=621"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/621\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/media?parent=621"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=621"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=621"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}