← Back to jQuery Course | Chapter 2: Selectors | Lesson 7 of 10

Visibility Filter Selectors

visible filter उन elements को select करता है जो visible हैं।
Syntax
javascript
$("selector:visible")
$("selector:hidden")

Visible Elements

':visible' सिर्फ उन elements को रखता है जो इस समय layout space लेते हैं -- यह display:none से hidden या render tree से हटाई गई किसी भी चीज़ को exclude करता है। zero width और height वाला एक element, या जिसका कोई ancestor hidden हो, यह visibility check भी fail करता है।

उदाहरण: Visible Elements

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p>Shown</p><p style="display:none;">Hidden</p>
    <script>
      console.log($("p:visible").length);
    </script>
  </body>
</html>

Hidden Elements

':hidden' :visible का उल्टा है -- यह display:none set वाले elements को match करता है, type="hidden" वाले form elements के साथ, लेकिन सिर्फ visibility:hidden या opacity:0 से styled elements को नहीं।

उदाहरण: Hidden Elements

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p>Shown</p><p style="display:none;">Hidden</p>
    <script>
      console.log($("p:hidden").length);
    </script>
  </body>
</html>

Hidden Items दिखाना

':hidden' से select करना और result पर .show() call करना उस content को reveal करने का एक common pattern है जो hidden शुरू हुआ था, जैसे कोई accordion panel या एक collapsed menu। इस तरह visible और hidden states के बीच toggle करना ज़्यादातर simple show/hide UI interactions का आधार है।

उदाहरण: Show Hidden Items

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p style="display:none;">Panel</p>
    <button id="btn">Show</button>
    <script>
      $("#btn").on("click", function() {
        $("p:hidden").show();
      });
    </script>
  </body>
</html>

Visible Items छुपाना

':visible' से select करना और .hide() call करना आपको किसी group में सिर्फ currently-shown elements को छुपाने देता है, पहले से hidden वाले को untouched छोड़ते हुए। यह pairing आपको बिल्कुल उन elements को hide करने का precise control देती है जो user इस समय देख सकता है, और कुछ नहीं।

उदाहरण: Hide Visible Items

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p>A</p><p style="display:none;">B</p>
    <button id="btn">Hide</button>
    <script>
      $("#btn").on("click", function() {
        $("p:visible").hide();
      });
    </script>
  </body>
</html>

Classes के साथ Visibility

Visibility filters class या attribute selectors के साथ combine होते हैं, जैसे '.item:visible', किसी बड़े group को सिर्फ उस subset तक narrow करने के लिए जो इस समय screen पर है। इस तरह filters को chain करना आपको एक broad group को exactly current UI state से relevant elements तक scope करने देता है।

उदाहरण: Visibility with Classes

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p class="item">Shown</p><p class="item" style="display:none;">Hidden</p>
    <script>
      console.log($(".item:visible").length);
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि :hidden सिर्फ display:none elements को match करता है; यह type="hidden" inputs और बिना layout size वाले elements को भी match करता है।
  2. यह उम्मीद करना कि :visibility:hidden या opacity:0 वाले elements के लिए :visible true होगा; :visible यह देखता है कि element जगह लेता है या नहीं, इसलिए वे फिर भी visible count होते हैं।
  3. :hidden items पर .show() call करना और उम्मीद करना कि यह किसी parent element में भी बैठे hiding rule को override कर देगा, जबकि parent hidden होने पर child hidden ही रहता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.