← Back to jQuery Course | Chapter 5: DOM Manipulation | Lesson 5 of 9

किसी Element के बाहर Content Insert करना

after() selected element के बाद content insert करता है।
Syntax
javascript
$(selector).before(content);
$(selector).after(content);

after()

after() method नए content को selected element के अंदर के बजाय उसके तुरंत बाद एक sibling के रूप में insert करता है, इसके बाहर। append() के उलट, नया content parent की child list में एक sibling बन जाता है, selected element का खुद का child नहीं।

उदाहरण: after()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="ref">Reference</p>
    <script>
      $("#ref").after("<p>After content</p>");
    </script>
  </body>
</html>

before()

before() content को selected element से तुरंत पहले एक preceding sibling के रूप में insert करता है। यह after() का mirror image है, तब उपयोगी जब नए content को किसी मौजूदा element के बाद के बजाय पहले दिखना हो।

उदाहरण: before()

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="ref">Reference</p>
    <script>
      $("#ref").before("<p>Before content</p>");
    </script>
  </body>
</html>

मौजूदा Elements Insert करना

after() और before() मौजूदा elements को DOM में एक नई position पर move कर सकते हैं, बिल्कुल जैसे append() और prepend() कर सकते हैं। इस तरह किसी element को move करना automatically इसे उसकी पिछली location से हटा देता है।

उदाहरण: Insert Existing Elements

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <div id="a"><p id="moveMe">Move me</p></div>
    <p id="ref">Reference</p>
    <script>
      $("#ref").after($("#moveMe"));
    </script>
  </body>
</html>

Messages जोड़ना

ये methods मौजूदा content के आस-पास notices जोड़ने के लिए उपयोगी हैं, जैसे किसी invalid input field के तुरंत बाद एक validation error message insert करना। message को एक sibling के रूप में रखना इसे उस element से visually और structurally जुड़ा रखता है जिससे यह related है।

उदाहरण: Add Messages

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input id="email" type="text">
    <script>
      $("#email").after("<span class='error'>Invalid email</span>");
    </script>
  </body>
</html>

Position मायने रखता है

before() content को document order में किसी element से पहले रखता है; after() इसे तुरंत बाद रखता है। क्योंकि दोनों element के अंदर के बजाय बाहर insert करते हैं, वे सिर्फ उन elements पर काम करते हैं जिनका पहले से एक parent है जिससे sibling attach हो सके।

उदाहरण: Position Matters

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <p id="ref">Reference</p>
    <script>
      $("#ref").before("<p>Before</p>").after("<p>After</p>");
    </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. जब आपको एक sibling चाहिए था तब .append() इस्तेमाल करना, इसलिए $("#ref").append("<p>...</p>") paragraph को #ref के बाद के बजाय उसके अंदर डाल देता है।
  2. .before() और .after() को confuse करना, नए element को reference के गलत तरफ़ रखते हुए।
  3. किसी ऐसे selector पर .after() call करना जो किसी से भी match नहीं करता, जो कुछ नहीं करता और कोई error नहीं देता।

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.