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

Siblings को Forward Traverse करना

next() ठीक बाद वाला sibling select करता है।
Syntax
javascript
$(selector).next();
$(selector).nextAll();
$(selector).nextUntil(selector);

next()

.next() उसी level पर selected element के ठीक बाद आने वाला single sibling element return करता है, या अगर कोई नहीं है तो एक empty set। यह हमेशा ज़्यादा से ज़्यादा एक element return करता है, चाहे कितने भी siblings बाद में हों।

उदाहरण: next()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="first">First</p><p id="second">Second</p>
    <script>
      console.log($("#first").next().text());
    </script>
  </body>
</html>

nextAll()

nextAll() selected element के बाद आने वाले हर sibling को select करता है, उसके parent के children के अंत तक, सिर्फ बिल्कुल अगले वाले को नहीं। यह तब उपयोगी है जब आपको किसी दिए गए point के बाद elements की पूरी series पर action लेना हो।

उदाहरण: nextAll()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p>One</p><p id="start">Two</p><p>Three</p><p>Four</p>
    <script>
      console.log($("#start").nextAll().length);
    </script>
  </body>
</html>

nextUntil()

nextUntil() बाद वाले siblings select करता है लेकिन अंत तक collect करने के बजाय आपके specify किए गए किसी matching element तक पहुँचने से ठीक पहले रुक जाता है। यह आपको दो known points के बीच exactly siblings लेने देता है।

उदाहरण: nextUntil()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="start">Start</p><p>Mid 1</p><p>Mid 2</p><p id="end">End</p>
    <script>
      console.log($("#start").nextUntil("#end").length);
    </script>
  </body>
</html>

Filters के साथ next

आप next(), nextAll(), या nextUntil() को एक selector के साथ combine करके सिर्फ किसी certain type के siblings को match कर सकते हैं, जिनकी परवाह नहीं उन्हें filter out करते हुए। यह बाद में हर returned element का tag या class manually check करने से बचाता है।

उदाहरण: next with Filters

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="start">Start</p><span>Not a p</span><p>Next p</p>
    <script>
      console.log($("#start").nextAll("p").length);
    </script>
  </body>
</html>

Forward Traversal Practice

Forward sibling traversal step-based layouts और lists के लिए उपयोगी है, जैसे किसी multi-step form में currently active वाले के बाद के हर step को highlight करना। क्योंकि ये methods सिर्फ आगे देखते हैं, वे naturally 'इस point के बाद की हर चीज़' express करते हैं।

उदाहरण: Forward Traversal Practice

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
    <style>
      .step { display: inline-block; padding: 6px 10px; border: 1px solid #999; }
      .step.active { background: #4a90d9; color: #fff; }
      .step.upcoming { background: #ffe082; }
    </style>
  </head>
  <body>
    <div class="step active">1</div><div class="step">2</div><div class="step">3</div>
    <script>
      $(".step.active").nextAll(".step").addClass("upcoming");
    </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. यह उम्मीद करना कि .next() हर बाद वाला sibling return करेगा, जबकि यह सिर्फ बिल्कुल अगले वाले को return करता है; सबके लिए .nextAll() इस्तेमाल करें।
  2. यह मान लेना कि .nextUntil("#stop") में #stop से match करने वाला element शामिल है, जबकि यह उससे ठीक पहले रुकता है और उसे छोड़ देता है।
  3. आख़िरी sibling पर .next() call करना और फिर .addClass("x") जैसे actions chain करना, जो चुपचाप कुछ नहीं करता क्योंकि set खाली है।

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.