← Back to jQuery Course | Chapter 7: DOM Traversing | Lesson 6 of 10

Nodes को Filter करना

filter() उन elements को रखता है जो एक condition से match करते हैं।
Syntax
javascript
$(selector).first();
$(selector).last();
$(selector).eq(index);
$(selector).filter(selector);
$(selector).not(selector);

filter()

.filter() किसी मौजूदा jQuery selection को सिर्फ उन elements तक narrow करता है जो आपके दिए गए selector या test function को satisfy करते हैं। find() के उलट, यह कभी descendants को नहीं देखता -- यह सिर्फ current selection में पहले से मौजूद elements को रखता या हटाता है।

उदाहरण: filter()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <li>One</li><li class="done">Two</li><li class="done">Three</li>
    <script>
      console.log($("li").filter(".done").length);
    </script>
  </body>
</html>

not()

not() current selection से किसी selector से match करने वाले elements हटाता है, effectively filter() का उल्टा। यह तब उपयोगी है जब यह describe करना आसान हो कि किन elements को exclude करना है बजाय किन्हें रखना है।

उदाहरण: not()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <li>One</li><li class="done">Two</li>
    <script>
      console.log($("li").not(".done").length);
    </script>
  </body>
</html>

eq()

eq() current selection के अंदर एक specific zero-based index पर element select करता है, किसी array में index करने जैसा। एक negative index pass करना शुरुआत के बजाय selection के अंत से count करता है।

उदाहरण: eq()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <li>One</li><li>Two</li><li>Three</li>
    <script>
      console.log($("li").eq(1).text());
    </script>
  </body>
</html>

first() और last()

first() और last() दो सबसे common filtering tasks -- सिर्फ पहला या आख़िरी matched element लेना -- को eq(0) या एक length calculation की ज़रूरत के बिना जल्दी लिखने लायक बनाते हैं। दोनों equivalent eq() call के shorthand हैं।

उदाहरण: first() and last()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <li>One</li><li>Two</li><li>Three</li>
    <script>
      console.log($("li").first().text(), $("li").last().text());
    </script>
  </body>
</html>

Filtering Practice

Filtering आपको एक बड़ी selection में से सिर्फ उन elements के साथ काम करने में मदद करता है जिनकी असल में आपको ज़रूरत है, जैसे सभी list items को सिर्फ completed marked वालों तक narrow करना। यह बाद के code को एक पहले से precise set पर act करके simpler रखता है।

उदाहरण: Filtering Practice

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <li class="done">Task 1</li><li>Task 2</li>
    <script>
      console.log($("li").filter(".done").length + " completed");
    </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. .filter("li") को .find("li") के साथ confuse करना; .filter() current set को narrow करता है जबकि .find() descendants के लिए उसके अंदर search करता है।
  2. यह भूल जाना कि .eq() zero-based है, इसलिए $("li").eq(1) दूसरा item select करता है, पहला नहीं।
  3. .not(".done") इस्तेमाल करना और उम्मीद करना कि यह page बदल देगा, जबकि यह सिर्फ एक छोटी jQuery selection return करता है और DOM से कोई elements नहीं हटाता।

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.