{"id":874,"date":"2012-06-08T12:50:58","date_gmt":"2012-06-08T12:50:58","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=874"},"modified":"2012-06-08T12:50:58","modified_gmt":"2012-06-08T12:50:58","slug":"jquery-selector-exists-check-if-selector-exists-in-jquery-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-selector-exists-check-if-selector-exists-in-jquery-examples\/","title":{"rendered":"jQuery Selector Exists Examples"},"content":{"rendered":"<p>Did you ever come across a requirement where you needed to check if selector exists in jQuery? In this article, I am going to share with you different ways to <em><strong>check if selector exists or not using jQuery<\/strong> <\/em>with examples.<!--more--><\/p>\n<h2>How to use jQuery to Check if Selector Exists Or Not &#8211; Examples<\/h2>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=874\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<p><em><strong>We can find out whether a selector exists or not in jQuery using the .length property<\/strong><\/em>. I am using the following HTML source code for the examples. The examples assume that there is already a div with ID &#8220;my_div&#8221; and we are checking to see if it exists along with a check for a non-existent div with ID &#8220;my_div1&#8221;.<\/p>\n<p><strong>Common HTML source code for all following examples:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Check if Selector Exists or Not&lt;\/title&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/jquery_1.7.1_min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n&lt;div id=\"my_div\" class=\"my_class\"&gt;Some content in the div.&lt;\/div&gt;\n\n&lt;button id=\"check\"&gt;Check&lt;\/button&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<h3>Example 1:\u00a0Check if Selector Exists in jQuery using length<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$(document).ready(function() {\n\n$(\"#check\").click( function() {\n\n\/\/A simple way to check for the length of the selector\n\nif( $(\"#my_div\").length )\n{\nalert('Yes');\n}\n\n\/\/A negation used in conjunction with the length property to check for existence of the selector\n\nif( !$(\"#my_div1\").length )\n{\nalert('No');\n}\n});\n\n});\n\n&lt;\/script&gt;<\/pre>\n<h3>Example 2:\u00a0Check if Selector Exists in jQuery using length (alternate)<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$(document).ready(function() {\n\n$(\"#check\").click( function() {\n\n\/\/If the length is more than 0, then the selector exists\n\nif( $(\"#my_div\").length &gt; 0 )\n{\nalert('Yes');\n}\n\n\/\/If the length is 0, then the selector does not exist\n\nif( $(\"#my_div1\").length == 0 )\n{\nalert('No');\n}\n});\n\n});\n\n&lt;\/script&gt;<\/pre>\n<p>You can use any of the above ways to <strong>check if a selector exist or not in jQuery<\/strong>.<\/p>\n<div class=\"make_demo\">\r\n\r\n\t<form action=\"https:\/\/theextremewebdesigns.com\/blog\/wp-content\/themes\/ewd_blog_2017\/try_demo.php?post_id=874\" method=\"post\" name=\"form1\" target=\"_blank\" id=\"form1\">\r\n\r\n\r\n\t<input type=\"hidden\" name=\"shortcode_content\" id=\"shortcode_content\" value=\"\" \/>\r\n\r\n        <div class=\"try_demo_btn_container\">\r\n\r\n\r\n            <button type=\"submit\" name=\"sbt_make_demo\" id=\"sbt_make_demo\" class=\"btn-primary btn-try-demo\">\r\n                Try Demo\r\n            <\/button>\r\n\r\n\r\n        <\/div><!-- .try_demo_btn_container -->\r\n\r\n    <\/form>\r\n    <\/div>\r\n    \n<h5><span style=\"font-size: 0.83em; line-height: 19px;\">Your Turn!<\/span><\/h5>\n<p>Do you know of any other ways to use <em><strong>jQuery to check if selector exists or not<\/strong><\/em>? Share your thoughts with us.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did you ever come across a requirement where you needed to check if selector exists in jQuery? In this article, I am going to share with you different ways to check if selector exists or not using jQuery with examples.<\/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-874","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-e6","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/874","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=874"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/874\/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=874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}