{"id":890,"date":"2012-06-09T05:01:04","date_gmt":"2012-06-09T05:01:04","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=890"},"modified":"2012-06-09T05:01:04","modified_gmt":"2012-06-09T05:01:04","slug":"jquery-check-attribute-check-if-attribute-exists-or-not-jquery","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-check-attribute-check-if-attribute-exists-or-not-jquery\/","title":{"rendered":"jQuery Check Attribute Exists Or Not Examples"},"content":{"rendered":"<p>Have you ever wanted to\u00a0<em><strong>check if attribute exists or not using jQuery<\/strong><\/em> for a certain element? In this article, I am going to share with you different ways that you can use to <strong>check if specific attribute for an element exists or not using jQuery<\/strong>. Examples follow.<!--more--><\/p>\n<h2>How to use jQuery to Check Attribute 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=890\" 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>For example purposes, I am assuming that I have a div in a page with ID and class. I am now going to check if this div has my desired attribute or not when &#8220;Check&#8221; button is clicked.<\/p>\n<p><strong>Common HTML Source code for all the following examples:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;Change Attribute exists or not&lt;\/title&gt;\n&lt;script type=\"text\/javascript\" src=\"js\/jquery-1.8.2.min.js\"&gt;&lt;\/script&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n\n&lt;div id=\"my_div\" class=\"my_class\"&gt;This is some content.&lt;\/div&gt;\n\n&lt;input type=\"submit\" name=\"check\" id=\"check\" value=\"Check\" \/&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<h3>Example 1: Check if Attribute exists or not using ID<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$('#check_using_id').click(function() {\n\nif($(\"#my_div[class]\").length)\n{\nalert('Attribute Exists!');\n}\nelse\n{\nalert('Attribute DOES NOT Exist!');\n}\n\n});\n\n&lt;\/script&gt;<\/pre>\n<h3>Example 2: Check if Attribute exists or not using Class<\/h3>\n<pre class=\"lang:xhtml decode:true\">&lt;script type=\"text\/javascript\"&gt;\n\n$('#check_using_class').click(function() {\n\nif($(\".my_class[class]\").length)\n{\nalert('Attribute Exists!');\n}\nelse\n{\nalert('Attribute DOES NOT Exist!');\n}\n\n});\n\n&lt;\/script&gt;<\/pre>\n<p>As you can see, you can target any attribute by enclosing them in square brackets as indicated in the above examples.<\/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=890\" 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>Simple, isn&#8217;t it?<\/h5>\n<p>Do you know of any other ways to <strong>check attribute exists or not URL using jQuery<\/strong>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever wanted to\u00a0check if attribute exists or not using jQuery for a certain element? In this article, I am going to share with you different ways that you can use to check if specific attribute for an element exists or not using jQuery. Examples follow.<\/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":[42,41],"class_list":["post-890","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jquery-tips-and-tricks","category-tips-and-tricks","tag-jquery-demos-2","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-em","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/890","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=890"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/890\/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=890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}