{"id":226,"date":"2010-03-22T12:12:19","date_gmt":"2010-03-22T18:12:19","guid":{"rendered":"http:\/\/www.n8williams.com\/devblog\/?p=226"},"modified":"2010-03-22T12:59:46","modified_gmt":"2010-03-22T18:59:46","slug":"jquery-checking-if-a-selector-worked-or-is-valid","status":"publish","type":"post","link":"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid","title":{"rendered":"jQuery checking if a selector worked or is valid"},"content":{"rendered":"<p>So if a jQuery selector failed to match it used to return the document at the root level of the DOM. Now it will return an empty set. What is an empty set? If you do var test = jQuery(); you&#8217;ll get the thing &#8212; an empty jQuery wrapper object that appears around every selected element when using jQuery selectors. <\/p>\n<p>So, if you do $(&#8216;#bogusId)&#8217;, you should get a jQuery object with a length of zero. I think that is the best way I have seen to tell if nothing was matched, just check for a length of zero on the result of calling something that doesn&#8217;t match.<\/p>\n<p>if($(&#8216;#bogusID&#8217;).length ==0)<\/p>\n<p>OR <\/p>\n<p>var myjQueryElement = $(&#8216;#bogusID&#8217;);<br \/>\nif(myjQueryElement.length == 0 )<\/p>\n<p>You can also use size()<\/p>\n<p>See <a href=\"http:\/\/api.jquery.com\/jQuery\/\" target=\"_blank\">http:\/\/api.jquery.com\/jQuery\/<\/a><\/p>\n<p>&#8216;As of jQuery 1.4, calling the jQuery() method with no arguments returns an empty jQuery set. In previous versions of jQuery, this would return a set containing the document node.&#8217;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So if a jQuery selector failed to match it used to return the document at the root level of the DOM. Now it will return an empty set. What is an empty set? If you do var test = jQuery(); you&#8217;ll get the thing &#8212; an empty jQuery wrapper object that appears around every selected &hellip; <a href=\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;jQuery checking if a selector worked or is valid&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.11 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>jQuery checking if a selector worked or is valid - The Dev Pages<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"jQuery checking if a selector worked or is valid - The Dev Pages\" \/>\n<meta property=\"og:description\" content=\"So if a jQuery selector failed to match it used to return the document at the root level of the DOM. Now it will return an empty set. What is an empty set? If you do var test = jQuery(); you&#8217;ll get the thing &#8212; an empty jQuery wrapper object that appears around every selected &hellip; Continue reading &quot;jQuery checking if a selector worked or is valid&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid\" \/>\n<meta property=\"og:site_name\" content=\"The Dev Pages\" \/>\n<meta property=\"article:published_time\" content=\"2010-03-22T18:12:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2010-03-22T18:59:46+00:00\" \/>\n<meta name=\"author\" content=\"Nate Admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@admin\" \/>\n<meta name=\"twitter:site\" content=\"@admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nate Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid#article\",\"isPartOf\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid\"},\"author\":{\"name\":\"Nate Admin\",\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\"},\"headline\":\"jQuery checking if a selector worked or is valid\",\"datePublished\":\"2010-03-22T18:12:19+00:00\",\"dateModified\":\"2010-03-22T18:59:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid\"},\"wordCount\":171,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\"},\"articleSection\":[\"Javascript and AJAX\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid\",\"url\":\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid\",\"name\":\"jQuery checking if a selector worked or is valid - The Dev Pages\",\"isPartOf\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#website\"},\"datePublished\":\"2010-03-22T18:12:19+00:00\",\"dateModified\":\"2010-03-22T18:59:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/n8williams.com\/devblog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"jQuery checking if a selector worked or is valid\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/n8williams.com\/devblog\/#website\",\"url\":\"https:\/\/n8williams.com\/devblog\/\",\"name\":\"The Dev Pages\",\"description\":\"A knowledge base for web applications development (and beyond)\",\"publisher\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/n8williams.com\/devblog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757\",\"name\":\"Nate Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g\",\"caption\":\"Nate Admin\"},\"logo\":{\"@id\":\"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/n8williams.com\",\"https:\/\/twitter.com\/admin\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"jQuery checking if a selector worked or is valid - The Dev Pages","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid","og_locale":"en_US","og_type":"article","og_title":"jQuery checking if a selector worked or is valid - The Dev Pages","og_description":"So if a jQuery selector failed to match it used to return the document at the root level of the DOM. Now it will return an empty set. What is an empty set? If you do var test = jQuery(); you&#8217;ll get the thing &#8212; an empty jQuery wrapper object that appears around every selected &hellip; Continue reading \"jQuery checking if a selector worked or is valid\"","og_url":"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid","og_site_name":"The Dev Pages","article_published_time":"2010-03-22T18:12:19+00:00","article_modified_time":"2010-03-22T18:59:46+00:00","author":"Nate Admin","twitter_card":"summary_large_image","twitter_creator":"@admin","twitter_site":"@admin","twitter_misc":{"Written by":"Nate Admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid#article","isPartOf":{"@id":"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid"},"author":{"name":"Nate Admin","@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757"},"headline":"jQuery checking if a selector worked or is valid","datePublished":"2010-03-22T18:12:19+00:00","dateModified":"2010-03-22T18:59:46+00:00","mainEntityOfPage":{"@id":"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid"},"wordCount":171,"commentCount":0,"publisher":{"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757"},"articleSection":["Javascript and AJAX"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid#respond"]}]},{"@type":"WebPage","@id":"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid","url":"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid","name":"jQuery checking if a selector worked or is valid - The Dev Pages","isPartOf":{"@id":"https:\/\/n8williams.com\/devblog\/#website"},"datePublished":"2010-03-22T18:12:19+00:00","dateModified":"2010-03-22T18:59:46+00:00","breadcrumb":{"@id":"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/n8williams.com\/devblog\/javascript_and_ajax\/jquery-checking-if-a-selector-worked-or-is-valid#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/n8williams.com\/devblog\/"},{"@type":"ListItem","position":2,"name":"jQuery checking if a selector worked or is valid"}]},{"@type":"WebSite","@id":"https:\/\/n8williams.com\/devblog\/#website","url":"https:\/\/n8williams.com\/devblog\/","name":"The Dev Pages","description":"A knowledge base for web applications development (and beyond)","publisher":{"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/n8williams.com\/devblog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/1c31624786b5382f1a811f0a01985757","name":"Nate Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/19d7bc7602072ac846e912622704a628?s=96&d=mm&r=g","caption":"Nate Admin"},"logo":{"@id":"https:\/\/n8williams.com\/devblog\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/n8williams.com","https:\/\/twitter.com\/admin"]}]}},"_links":{"self":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/posts\/226"}],"collection":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/comments?post=226"}],"version-history":[{"count":2,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/posts\/226\/revisions"}],"predecessor-version":[{"id":228,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/posts\/226\/revisions\/228"}],"wp:attachment":[{"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/media?parent=226"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/categories?post=226"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/n8williams.com\/devblog\/wp-json\/wp\/v2\/tags?post=226"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}