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

Form Filter Selectors

checked filter checked checkboxes और radio buttons select करता है।
Syntax
javascript
$("input:enabled")
$("input:disabled")
$("input:checked")
$("option:selected")

Checked Inputs

':checked' किसी भी checkbox या radio input को match करता है जिसकी checked property इस समय true है, आपको selected options सीधे ढूँढने देते हुए। यह user के form के साथ interact करने पर live update होता है, सिर्फ page load पर नहीं।

उदाहरण: Checked Inputs

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input type="checkbox" checked><input type="checkbox">
    <script>
      console.log($(":checked").length);
    </script>
  </body>
</html>

Selected Options

':selected' किसी <select> के अंदर उन <option> elements को target करता है जो इस समय user द्वारा चुने गए हैं, जो dropdown menus के लिए :checked के बराबर है। यह filter सिर्फ किसी select element के अंदर ही मतलब रखता है, क्योंकि plain inputs में selected option का कोई concept नहीं है।

उदाहरण: Selected Options

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <select><option>A</option><option selected>B</option></select>
    <script>
      console.log($(":selected").text());
    </script>
  </body>
</html>

Enabled Controls

':enabled' उन form controls को match करता है जो इस समय user input accept कर सकते हैं, disabled attribute set वाले किसी भी element को exclude करते हुए। यह अक्सर दोनों states को अलग तरीके से style या handle करने के लिए :disabled के साथ जोड़ा जाता है।

उदाहरण: Enabled Controls

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input><input disabled>
    <script>
      console.log($(":enabled").length);
    </script>
  </body>
</html>

Disabled Controls

':disabled' :enabled का mirror है -- यह उन controls को match करता है जिनके साथ user इस समय interact नहीं कर सकता, अक्सर script के ज़रिए उन्हें visually अलग दिखाने या फिर से enable करने के लिए इस्तेमाल होता है। Disabled form controls को focus भी नहीं मिल सकता या form submit होने पर शामिल नहीं किए जा सकते, यह filter आपके scripts में इसे account में लेने में मदद करता है।

उदाहरण: Disabled Controls

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input><input disabled>
    <script>
      console.log($(":disabled").length);
    </script>
  </body>
</html>

Focus Filter

':focus' उस single element को match करता है जिसके पास इस समय keyboard focus है, exactly उस field से जुड़ी styles या logic लागू करने के लिए उपयोगी जिसमें user actively type कर रहा है। यह आमतौर पर manually focus track किए बिना उस field को validate या highlight करने के लिए इस्तेमाल होता है जिसे user इस समय भर रहा है।

उदाहरण: Focus Filter

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input id="a"><input id="b">
    <script>
      $("#a").focus();
      console.log($(":focus").attr("id"));
    </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. checked checkboxes ढूँढने के लिए :selected इस्तेमाल करना, जबकि यह <option> elements पर काम करता है और :checked checkboxes व radios के लिए है।
  2. एक बार :checked select करना और result के update होने की उम्मीद करना, जबकि एक jQuery object एक snapshot है और user के कुछ बदलने के बाद selector को फिर से चलाना ज़रूरी है।
  3. :disabled को readonly attribute के साथ confuse करना, क्योंकि एक readonly input फिर भी :enabled होता है।

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.