{"id":3067,"date":"2013-03-14T11:33:16","date_gmt":"2013-03-14T11:33:16","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3067"},"modified":"2013-03-14T11:33:16","modified_gmt":"2013-03-14T11:33:16","slug":"jquery-empty-selector-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-empty-selector-examples\/","title":{"rendered":"jQuery empty Selector Examples"},"content":{"rendered":"<p>Ever wondered how to use the <em><strong>jQuery empty selector<\/strong><\/em>? In this article, I am going to share different ways of using the empty selector in jQuery along with easy to follow examples. Read on to find out more.<\/p>\n<p><!--more--><\/p>\n<h2>How to use jQuery empty 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=3067\" 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>Let&#8217;s\u00a0assume that we have different html elements on the web page. such as Table. What essentially the :empty selector does is that it select all elements that have no children (including text nodes).\u00a0So let&#8217;s see <em><strong>how to use :empty<\/strong><\/em>:<\/p>\n<h3>Example 1: Use :empty on Table to alert \/get all empty cells of the table<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 1\">&lt;style type=\"text\/css\"&gt;\ntable td {\n\tborder: 1px solid #CCC;\t\n}\n.cell_class {\n\tbackground-color: #D0F5FF;\n\tborder: 1px solid #06F;\n}\n&lt;\/style&gt;\n\n&lt;table width=\"50%\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\" id=\"table1\"&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;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;&lt;\/td&gt;\n    &lt;td&gt;R2C2&lt;\/td&gt;\n    &lt;td&gt;&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;&lt;\/td&gt;\n    &lt;td&gt;&lt;\/td&gt;\n    &lt;td&gt;R3C3&lt;\/td&gt;\n  &lt;\/tr&gt;\n&lt;\/table&gt;\n\n&lt;p&gt;\n&lt;input type=\"submit\" name=\"sbt_get_all_empty_cells\" id=\"sbt_get_all_empty_cells\" value=\"Get all Empty Cells of Table\"&gt;\n&lt;\/p&gt;\n\n\t$('#sbt_get_all_empty_cells').on(\"click\", function(e){\t\n\n\t\t$('#table1 td:empty').each(function(index) {\n\n\t\t\tvar row_index = $(this).parent().index('tr');\n\n\t\t\tvar col_index = $(this).index('tr:eq('+row_index+') td');\n\n\t\t\talert('Row Number # '+(row_index + 1)+' Column Number # '+(col_index + 1) + ' is Empty!');\n\n\t\t});\t\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<p>When you run the above code, simply click the &#8220;Get all Empty Cells of Table&#8221; button and that will alert all the empty cell Row &amp; Column numbers. You can apply whatever manipulations you want by using them.<\/p>\n<h3>Example 2: Use :empty on Table to Write Custom Data To Empty Cells<\/h3>\n<p>There are different parts in this example, so let&#8217;s take a look at each of these parts separately.\u00a0<strong>The following is the common HTML source for all these parts<\/strong>.<\/p>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 2\">&lt;style type=\"text\/css\"&gt;\ntable td {\n\tborder: 1px solid #CCC;\t\n}\n.cell_class {\n\tbackground-color: #D0F5FF;\n\tborder: 1px solid #06F;\n}\n&lt;\/style&gt;\n\n&lt;table width=\"50%\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\" id=\"table2\"&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;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;&lt;\/td&gt;\n    &lt;td&gt;R2C2&lt;\/td&gt;\n    &lt;td&gt;&lt;\/td&gt;\n  &lt;\/tr&gt;\n  &lt;tr&gt;\n    &lt;td&gt;&lt;\/td&gt;\n    &lt;td&gt;&lt;\/td&gt;\n    &lt;td&gt;R3C3&lt;\/td&gt;\n  &lt;\/tr&gt;\n&lt;\/table&gt;\n\n&lt;p&gt;\n&lt;input type=\"submit\" name=\"sbt_write_to_empty_cells\" id=\"sbt_write_to_empty_cells\" value=\"Write to all Empty Cells of Table 2\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_revert_to_empty_cells\" id=\"sbt_revert_to_empty_cells\" value=\"Revert To Empty Cells of Table 2\"&gt;\n&lt;\/p&gt;\n\n&lt;p&gt;\n&lt;input type=\"submit\" name=\"sbt_add_class_to_empty_cells\" id=\"sbt_add_class_to_empty_cells\" value=\"Add Class To Empty Cells of Table 2\"&gt;\n\n&lt;input type=\"submit\" name=\"sbt_remove_class_from_empty_cells\" id=\"sbt_remove_class_from_empty_cells\" value=\"Remove Class From Empty Cells of Table 2\"&gt;&lt;\/p&gt;<\/pre>\n<h3>Part 1: \u00a0Write to all Empty Cells of the Table<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Part 1:  Write to all Empty Cells of the Table\">\t$('#sbt_write_to_empty_cells').on(\"click\", function(e){\t\n\n\t\t$('#table2 td:empty').text(\"Cell is Empty!\");\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<p>When you click the button, all empty cells of the table will be filled with the custom text (Cell is Empty!).<\/p>\n<h3>Part 2: \u00a0Revert all \/ Make Table Cells by removing the custom text that you filled them with<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Part 2:  Revert all \/ Make Table Cells by removing the custom text that you filled them with\">\t$('#sbt_revert_to_empty_cells').on(\"click\", function(e){\t\n\n\t\t$('#table2 td').each(function(index) {\n\n\t\t\tif( 'Cell is Empty!' == $(this).text() )\n\t\t\t{\n\t\t\t\t$(this).text('');\n\t\t\t}\t\t\t\n\n\t\t});\t\t\t\t\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<h3>Part 3: \u00a0Add Custom Class to the Empty Cells of the Table<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Part 3:  Add Custom Class to the Empty Cells of the Table\">\t$('#sbt_add_class_to_empty_cells').on(\"click\", function(e){\t\n\n\t\t$('#table2 td:empty').addClass('cell_class');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<p>Add a class or multiple class of your choice to empty table cells by clicking the button.<\/p>\n<h3>Part 4: \u00a0Remove Custom Class that you applied to the Empty Cells of the Table<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Part 4:  Remove Custom Class that you applied to the Empty Cells of the Table\">\t$('#sbt_remove_class_from_empty_cells').on(\"click\", function(e){\t\n\n\t\t$('#table2 td:empty').removeClass('cell_class');\n\n\t\te.preventDefault();\n\n\t});<\/pre>\n<p>Click on the button to remove the class that you applied to the empty table cells.<\/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=3067\" 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>That&#8217;s it!<\/h5>\n<p>Do you know of any other ways to use <em><strong>jQuery empty selector<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever wondered how to use the jQuery empty selector? In this article, I am going to share different ways of using the empty selector in jQuery along with easy to follow examples. Read on to find out more.<\/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-3067","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-Nt","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3067","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=3067"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3067\/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=3067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}