{"id":3235,"date":"2013-03-25T19:05:15","date_gmt":"2013-03-25T19:05:15","guid":{"rendered":"https:\/\/theextremewebdesigns.com\/blog\/?p=3235"},"modified":"2013-03-25T19:05:15","modified_gmt":"2013-03-25T19:05:15","slug":"jquery-visible-selector-examples","status":"publish","type":"post","link":"https:\/\/theextremewebdesigns.com\/blog\/jquery-visible-selector-examples\/","title":{"rendered":"jQuery :visible Selector Examples"},"content":{"rendered":"<p>If you wish to check whether an element in a web page is visible or hidden, you can easily achieve it using the <em><strong>jQuery visible selector<\/strong><\/em>. In this article, I am going to share easy examples that show how to use the visible selector in making a div visible \/ hidden, check\u00a0if element is visible or not, set visible, toggle visibility, etc. Read on to find out more.<\/p>\n<p><!--more--><\/p>\n<h2>How to use the jQuery visible 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=3235\" 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>In order to see the usage of the <em><strong>:visible selector<\/strong><\/em>, assume that we have few elements in a web page such as a div, an image, etc. Let&#8217;s see how the visibility works and can be manipulated in real time. Check out the examples below.<\/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#my_div1  {\n\tbackground-color: #3C6;\n\tborder: 1px solid #090;\n}\n\n.custom_class {\n\tbackground-color: #FC9;\n\tborder: 1px solid #F90;\n}\t\n&lt;\/style&gt;\n\n\t&lt;div id=\"my_div1\"&gt;This is my existing Div 1.&lt;\/div&gt;\n\n\t&lt;p&gt;\n\t&lt;input type=\"submit\" name=\"sbt_check_div_visibility\" id=\"sbt_check_div_visibility\" value=\"Check if DIV 1 is Visible or Not\"&gt;    \n    &lt;\/p&gt;\n\n    &lt;div id=\"my_div2\"&gt;This is my existing Div 2.&lt;\/div&gt;\n\n\t&lt;p&gt;\n\t&lt;input type=\"submit\" name=\"sbt_toggle_div_visibility\" id=\"sbt_toggle_div_visibility\" value=\"Toggle DIV 2 Visibility\"&gt;    \n    &lt;\/p&gt;\n\n    &lt;img src=\"images\/money.jpg\" class=\"my_image\" \/&gt;   \n\n\t&lt;p&gt;\n\t&lt;input type=\"submit\" name=\"sbt_set_image_visibility\" id=\"sbt_set_image_visibility\" value=\"Set Image Visibility\"&gt;    &lt;\/p&gt;\n\n    &lt;ul&gt;\n    &lt;li id=\"list_item_1\"&gt;List Item 1&lt;\/li&gt;\n    &lt;li id=\"list_item_2\"&gt;List Item 2&lt;\/li&gt;    \n    &lt;li id=\"list_item_3\" style=\"visibility:hidden;\"&gt;List Item 3&lt;\/li&gt;\n    &lt;li id=\"list_item_4\"&gt;List Item 4&lt;\/li&gt;    \n    &lt;li id=\"list_item_5\" style=\"visibility:hidden;\"&gt;List Item 5&lt;\/li&gt;\n    &lt;li id=\"list_item_6\"&gt;List Item 6&lt;\/li&gt;            \n    &lt;\/ul&gt;\n\n\t&lt;p&gt;\n    &lt;input type=\"submit\" name=\"sbt_visible_li_count\" id=\"sbt_visible_li_count\" value=\"Alert Visible LIST ITEM ID\"&gt;\n    &lt;\/p&gt;   \n\n    &lt;p id=\"para1\" style=\"visibility: visible;\"&gt;This is a Paragraph 1.&lt;\/p&gt;    \n\n\t&lt;p&gt;\n\t&lt;input type=\"submit\" name=\"sbt_chk_p_visibility\" id=\"sbt_chk_p_visibility\" value=\"Check PARAGRAPH visibility using visibility attribute\"&gt;    \n    &lt;\/p&gt;\n\n\t&lt;div id=\"h6_container\"&gt;\n    This is H6 Div Container.\n    \t&lt;h6 style=\"visibility: hidden;\"&gt;This is a hidden H6 Heading&lt;\/h6&gt;\n    &lt;\/div&gt;\n\n\t&lt;p&gt;\n\t&lt;input type=\"submit\" name=\"sbt_create_if_not_visible\" id=\"sbt_create_if_not_visible\" value=\"Create New H6 if current H6 is invisible\"&gt;    \n\t&lt;\/p&gt;\n\n&lt;script type=\"text\/javascript\"&gt;\n\n$(document).ready(function(){\n\n\/\/Put all your jQuery code here\n\n});\n\n&lt;\/script&gt;<\/pre>\n<h3>Example 1: Check if div is visible or not visible (hidden)<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 1\">$('#sbt_check_div_visibility').on(\"click\", function(e){\n\n\tif( $(\"#my_div1:visible\") )\n\t{\n\t\talert('DIV 1 is Visible');\t\t\n\t}\n\telse\n\t{\n\t\talert('DIV 1 is Hidden');\t\t\t\t\n\t}\n\n\te.preventDefault();\n\n});<\/pre>\n<h3>Example 2: Toggle div visibility i.e Make a visible div hidden and vice versa<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 2\">$('#sbt_toggle_div_visibility').on(\"click\", function(e){\t\t\t\n\n\t$(\"#my_div2\").toggle();\n\n\te.preventDefault();\t\t\n\n});<\/pre>\n<h3>Example 3: Manually set visibility to an element, such as an image, div, etc.<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 3: Manually set visibility to an element, such as an image, div, etc.\">$('#sbt_set_image_visibility').on(\"click\", function(e){\t\t\t\n\n\t$(\".my_image\").css(\"visibility\", \"hidden\");\n\n\te.preventDefault();\n\n});<\/pre>\n<h3>Example 4: Select visible elements only for list items of unordered \u00a0i.e. if element is visible, then alert its (the visible list item) ID<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 4\">$('#sbt_visible_li_count').on(\"click\", function(e){\n\n\t$('ul li').each(function(index) {\n\n\t\tif( $(this).css(\"visibility\") !== 'hidden' )\n\t\t{\n\t    \talert( $(this).attr(\"id\") );\n\t\t}\n\n\t});\n\n\te.preventDefault();\n\n});<\/pre>\n<h3>Example 5: Check element visibility using the\u00a0.is() method<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 5\">$('#sbt_chk_p_visibility').on(\"click\", function(e){\t\t\t\n\n\tif( $(\"#para1\").is(':visible') )\n\t{\n\t\talert('Paragraph is VISIBLE');\n\t}\n\telse\n\t{\n\t\talert('Paragraph is HIDDEN');\t\t\n\t}\n\n\te.preventDefault();\t\t\n\n});<\/pre>\n<h3>Example 6: If element is not visible then create a new element<\/h3>\n<pre class=\"lang:xhtml decode:true\" title=\"Example 6\">$('#sbt_create_if_not_visible').on(\"click\", function(e){\t\t\t\n\n\tif( $(\"h6:hidden\") )\n\t{\n\t\t$('#h6_container').append('&lt;h6&gt;This is NEw H6 HEADING&lt;\/h6&gt;');\n\t}\n\n\te.preventDefault();\n\n});<\/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=3235\" 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 use the <em><span style=\"text-decoration: underline;\"><strong>jQuery\u00a0<\/strong><\/span><strong>visible selector<\/strong><\/em>? Feel free to suggest by commenting below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you wish to check whether an element in a web page is visible or hidden, you can easily achieve it using the jQuery visible selector. In this article, I am going to share easy examples that show how to use the visible selector in making a div visible \/ hidden, check\u00a0if element is visible [&hellip;]<\/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-3235","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-Qb","jetpack_sharing_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3235","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=3235"}],"version-history":[{"count":0,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/posts\/3235\/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=3235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/categories?post=3235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theextremewebdesigns.com\/blog\/wp-json\/wp\/v2\/tags?post=3235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}