{"id":3426,"date":"2013-03-27T19:05:24","date_gmt":"2013-03-27T19:05:24","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3426"},"modified":"2013-03-27T19:05:24","modified_gmt":"2013-03-27T19:05:24","slug":"jquery-selector-not-class","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-selector-not-class\/","title":{"rendered":"jquery :not() Selector Examples"},"content":{"rendered":"<p>The <em><strong>jQuery not selector<\/strong><\/em>\u00a0performs the opposite of what we generally to do find out if an element contains a specific value. In other words, it\u00a0selects all elements that do not match the given selector. That said, let&#8217;s take a look at how to use it in real time by means of easy examples.<\/p>\n<p><!--more--><\/p>\n<h2>How to use the jQuery not selector<\/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=3426\" 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>So for the sake of examples, let&#8217;s assume that we have different elements on a web page, such as divs, paragraphs, list items, table, checkboxes. We will see how to apply the :not()\u00a0selector\u00a0in various cases.<\/p>\n<p><strong>Common HTML Source code for all the following examples:<\/strong><\/p>\n<pre class=\"lang:xhtml decode:true\" title=\"Common HTML Source code for all the following examples:\">&lt;style type=\"text\/css\"&gt;\n.existing_yellow {\n\tbackground-color: #FFFF99;\n\tborder: 1px solid #FF0;\n}\n.existing_purple {\n\tbackground-color: #9999FF;\n\tborder: 1px solid #90F;\n}\n.green {\n\tbackground-color: #D7FFD7;\n\tborder: 1px solid #090;\n}\n.blue {\n\tbackground-color: #E1F3FF;\n\tborder: 1px solid #09F;\n}\n.red_text {\n\tcolor: #D70000;\n\tfont-weight: bold;\n\tfont-style: italic;\t\t\n}\n.orange {\t\n\tborder: 2px solid #F90;\n\tfont-family: Georgia, \"Times New Roman\", Times, serif;\n}\n&lt;\/style&gt;\n\n&lt;div class=\"divs_container\"&gt;\n\n&lt;p&gt;  \n    &lt;input name=\"subscribe\" type=\"checkbox\" id=\"subscribe_0\" value=\"Email\" checked=\"CHECKED\"&gt;\n   &lt;label for=\"subscribe_0\"&gt;Email&lt;\/label&gt;\n  &lt;br&gt;\n  &lt;label&gt;\n    &lt;input type=\"checkbox\" name=\"subscribe\" value=\"Newsletter\" id=\"subscribe_1\"&gt;\n   &lt;label for=\"subscribe_1\"&gt;Newsletter&lt;\/label&gt;\n  &lt;br&gt;\n  &lt;label&gt;\n    &lt;input type=\"checkbox\" name=\"subscribe\" value=\"RSS\" id=\"subscribe_2\"&gt;\n   &lt;label for=\"subscribe_2\"&gt;RSS&lt;\/label&gt;\n  &lt;br&gt;\n&lt;\/p&gt;\n\n&lt;div id=\"my_div1\"&gt;This is MY DIV 1\n\n    &lt;div id=\"my_div2\" class=\"existing_yellow red_text green\"&gt;This is MY DIV 2&lt;\/div&gt;\n\n    &lt;\/div&gt;\n\n  &lt;div id=\"my_div3\" class=\"existing_purple green\"&gt;This is MY DIV 3&lt;\/div&gt;    \n\n    &lt;div id=\"my_div4\" class=\"existing_yellow\"&gt;This is MY DIV 4&lt;\/div&gt;    \n\n\t&lt;p&gt;This is a PARA&lt;\/p&gt;    \n\n  &lt;ul&gt;\n    &lt;li&gt;High List item 1&lt;\/li&gt;\n    &lt;li&gt;List item 2&lt;\/li&gt;\n    &lt;li&gt;High List item 3&lt;\/li&gt;\n    &lt;li&gt;List item 4&lt;\/li&gt;\n    &lt;li&gt;High List item 5&lt;\/li&gt;\n  \t&lt;\/ul&gt;\n\n  &lt;table width=\"100%\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\"&gt;\n      &lt;tr&gt;\n        &lt;td&gt;R1C1&lt;\/td&gt;\n        &lt;td&gt;&lt;\/td&gt;\n        &lt;td&gt;R1C3&lt;\/td&gt;\n        &lt;td&gt;R1C4&lt;\/td&gt;\n      &lt;\/tr&gt;\n      &lt;tr&gt;\n        &lt;td&gt;R2C1&lt;\/td&gt;\n        &lt;td&gt;R2C2&lt;\/td&gt;\n        &lt;td&gt;R2C3&lt;\/td&gt;\n        &lt;td&gt;R2C4&lt;\/td&gt;\n      &lt;\/tr&gt;\n      &lt;tr&gt;\n        &lt;td&gt;R3C1&lt;\/td&gt;\n        &lt;td&gt;R3C2&lt;\/td&gt;\n        &lt;td&gt;&lt;\/td&gt;\n        &lt;td&gt;R3C4&lt;\/td&gt;\n      &lt;\/tr&gt;\n      &lt;tr&gt;\n        &lt;td&gt;R4C1&lt;\/td&gt;\n        &lt;td&gt;R4C2&lt;\/td&gt;\n        &lt;td&gt;R4C3&lt;\/td&gt;\n        &lt;td&gt;&lt;\/td&gt;\n      &lt;\/tr&gt;\n  &lt;\/table&gt;\n\n&lt;br&gt;\n\n  &lt;div id=\"hidden_div\" class=\"existing_yellow\" style=\"visibility: hidden;\"&gt;This is a hidden Div&lt;\/div&gt;\n\n&lt;div id=\"paras_container\" class=\"existing_purple\"&gt;\n  \t&lt;p&gt;This is para 1&lt;\/p&gt;\n  \t&lt;p&gt;This is para 2&lt;\/p&gt;\n  \t&lt;p&gt;This is para 3&lt;\/p&gt;\n  \t&lt;p&gt;This is para 4&lt;\/p&gt;            \n  &lt;\/div&gt;\n\n&lt;\/div&gt;\n\n&lt;p&gt;\t\n\n&lt;input type=\"submit\" name=\"sbt_sel_not_checked_checkboxes\" id=\"sbt_sel_not_checked_checkboxes\" value=\"Highlight Checkboxes that were not checked\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_disable_checkboxes\" id=\"sbt_disable_checkboxes\" value=\"Disable all Unchecked Checkboxes\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_alert_div_ids\" id=\"sbt_alert_div_ids\" value=\"Alert Div IDs that do not have GREEN class\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_list_item\" id=\"sbt_list_item\" value=\"Add GREEN class to all list items that do NOT have the word 'High' in their text\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_table_cells\" id=\"sbt_table_cells\" value=\"Add GREEN class to all Table cells that are NOT empty\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_header_not_found\" id=\"sbt_header_not_found\" value=\"Check for Header tags and alert result\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_para_not_found\" id=\"sbt_para_not_found\" value=\"Check for PARAGRAPH tags and alert result\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_div_visible\" id=\"sbt_div_visible\" value=\"Make a DIV visible if it is not visible\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_para_not_first\" id=\"sbt_para_not_first\" value=\"Add Orange Class to paragraphs that are NOT the first child in a DIV\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_para_not_last\" id=\"sbt_para_not_last\" value=\"Add Red Class to paragraphs that are NOT the last child in a DIV\"&gt;\n\n&lt;\/p&gt;\n\n&lt;script type=\"text\/javascript\" language=\"javascript\" &gt;\n\n$(document).ready(function(){\n\n\/\/Add jQuery code here\n\n});\t\n\n&lt;\/script&gt;<\/pre>\n<h3>Example 1:\u00a0Add GREEN class to labels of all checkboxes that are not checked\u00a0when a button is clicked<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 1\">\t$('#sbt_sel_not_checked_checkboxes').on(\"click\", function(e){\t\t\t\t\t\t\n\n\t\t$(\"input[name='subscribe']:not(:checked) + label\").addClass('green');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 2:\u00a0Disable all checkboxes that are not checked when a button is clicked<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 2\">\t$('#sbt_disable_checkboxes').on(\"click\", function(e){\t\t\n\n\t\t$(\"input[name='subscribe']:not(:checked)\").prop(\"disabled\", \"disabled\");\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 3:\u00a0If any divs do not have the green class applied to them, then alert their ID when a button is clicked<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 3\">\t$('#sbt_alert_div_ids').on(\"click\", function(e){\t\t\t\t\n\n\t\t$('.divs_container div:not(\".green\")').each(function(index) {\n\t\t    alert( $(this).attr('id') );\n\t\t});\t\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 4:\u00a0If the list item does not contain the word &#8216;High&#8217;, add green class to such items<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 4\">\t$('#sbt_list_item').on(\"click\", function(e){\t\t\t\t\n\n\t\t$('.divs_container ul li:not(:contains(\"High\")').addClass('green');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 5:\u00a0Add GREEN Class to all table cells that do not contain any data in them<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 5\">\t$('#sbt_table_cells').on(\"click\", function(e){\t\t\t\t\n\n\t\t$(\".divs_container table td:not(:empty)\").addClass('green');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 6: \u00a0If no header tags are found, alert the same<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 6\">\t$('#sbt_header_not_found').on(\"click\", function(e){\t\t\t\t\n\n\t\tif( $(\".divs_container :header\").length === 0 )\n\t\t{\n\t\t\talert('No Heading tags available');\t\n\t\t}\n\t\telse\n\t\t{\n\t\t\talert('Heading tags available');\t\t\n\t\t}\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 7:\u00a0If no paragraph tags are found, alert the same<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 7\">\t$('#sbt_para_not_found').on(\"click\", function(e){\t\t\t\t\n\n\t\tif( $(\".divs_container p\").length === 0 )\n\t\t{\n\t\t\talert('No Paragraph tags available');\t\n\t\t}\n\t\telse\n\t\t{\n\t\t\talert('Paragraph tags available');\t\t\n\t\t}\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 8:\u00a0If DIV is not visible (hidden), then make it visible<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 8\">\t$('#sbt_div_visible').on(\"click\", function(e){\t\t\t\t\n\n\t\tif( $(\"#hidden_div:not(:visible)\") )\n\t\t{\t\t\t\n\t\t\t$(\"#hidden_div\").css('visibility', 'visible');\n\t\t}\n\t\telse\n\t\t{\n\t\t\talert('Hidden Div is Visisble.');\t\t\n\t\t}\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 9:\u00a0Add ORANGE class to all paragraphs that are not the first child in a Div<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 9\">\t$('#sbt_para_not_first').on(\"click\", function(e){\t\t\t\t\n\n\t\t$(\"#paras_container p:not(':first')\").addClass('orange');\t\t\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Example 10:\u00a0Add RED_TEXT class to all paragraphs that are not the last child in a Div<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 10\">\t$('#sbt_para_not_last').on(\"click\", function(e){\t\t\t\t\n\n\t\t$(\"#paras_container p:not(':last')\").addClass('red_text');\t\t\n\n\t\te.preventDefault();\n\n\t});<\/pre>\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=3426\" 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>Your Turn!<\/h5>\n<p>Do you know of any other ways to use the <em><strong>jQuery :not() selector<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The jQuery not selector\u00a0performs the opposite of what we generally to do find out if an element contains a specific value. In other words, it\u00a0selects all elements that do not match the given selector. That said, let&#8217;s take a look at how to use it in real time by means of easy 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":[42,43,41],"class_list":["post-3426","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-snippets","tag-jquery-tips"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/paBnQX-Tg","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3426","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=3426"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3426\/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=3426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}