AD JB Toolbox 01 EN

jQuery .insertAfter(): Insert Content as the Next Sibling

.insertAfter() inserts content as the target’s next sibling. It can create new content from HTML or move an existing element to that position.

Syntax and behavior

$(content).insertAfter(target);

This is useful for inserting a new paragraph after a chosen target. Select or create the content before the method, then pass the destination as its argument. The Document Object Model (DOM) is the browser’s tree of document nodes. The inserted element shares a parent with the target; it is not placed inside the target. The returned jQuery object contains the inserted elements, so a chained method operates on them.

The official jQuery .insertAfter() documentation describes additional call signatures and argument types.

Complete runnable example

Save the following code as a UTF-8 example.html file and open it in a browser. It loads the full jQuery 4.0.0 build from an external URL, so an internet connection is required. The $(function () { ... }) callback waits for the document to be ready before running the example.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery insertAfter example</title>
  <style>
    body { margin: 24px; font-family: sans-serif; line-height: 1.6; }
    .marked { background: #fff0b3; border: 2px solid #835b00; }
    .active { color: #a00000; font-weight: bold; }
    button, input, select { font: inherit; }
    #output { white-space: pre-wrap; overflow-wrap: anywhere; }
  </style>
  <script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
<div id="container"><p id="target">Target paragraph</p></div>
  <p id="output" aria-live="polite"></p>
  <script>
    $(function () {
      $("<p class='new'>New paragraph</p>").insertAfter("#target");
      $("#output").text($("#container").children().map(function () { return $(this).text(); }).get().join(" / "));
    });
  </script>
</body>
</html>

Expected result

Opening the file inserts the new content once. The result paragraph shows Target paragraph / New paragraph. In the browser’s Elements panel, check that the new paragraph and the target have the same parent. Reloading the file starts again from the original markup.

Moving an existing element

$("#existing").insertAfter("#destination");

Use this snippet in a document containing #existing and #destination. With one destination, the existing node changes position or parent. Inspect the live DOM rather than the original page source to see the change.

Inserting input as plain text

const userText = "<Help text>";
$("<p>").text(userText).insertAfter("#target");

Replace the insertion line in the complete example with this snippet. The angle brackets appear as text. To undo the insertion, select only the newly added item and call .remove(), keeping the existing content outside that selection.

Important details and troubleshooting

  • Inserting an existing element into one destination moves the original node rather than copying it. Use .clone() before insertion if the original must remain in place.
  • With multiple destinations, jQuery creates copies for every destination except the last, which receives the original. Avoid cloning an id into several places; use classes or unique IDs.
  • If no destination matches, no inserted content appears in the document. Check the selector and when the script runs. A click handler that inserts content will add another item on each click.
  • Do not build HTML strings from untrusted input. This can introduce cross-site scripting (XSS). Create an element and use .text() to insert user-provided text without interpreting it as markup.

Compare the behavior with .after() sibling insertion, .insertBefore() with content first.

More in This Category
jQuery .toggleClass(): Toggle a Class or Set Its State

jQuery .toggleClass(): Toggle a Class or Set Its State

Distinguish reversing a class from setting a known Boolean state, with a toggle-button example and current syntax.

jQuery .find(): Select Matching Descendants

jQuery .find(): Select Matching Descendants

Search through descendant levels while excluding the container, and compare find with children, filter, and has.

jQuery .val(): Read and Set Form Field Values

jQuery .val(): Read and Set Form Field Values

Read and set current field values and multiple selections, and handle change events separately from value assignment.

jQuery .width(): Measure and Set Content Width

jQuery .width(): Measure and Set Content Width

Compare content, padding, and border widths, and verify how width getters and setters behave with border-box sizing.

jQuery .has(): Select Elements with Matching Descendants

jQuery .has(): Select Elements with Matching Descendants

Filter the original selection by matching descendants, and see why has and find return different elements.

jQuery .unwrap(): Remove a Parent and Keep Its Children

jQuery .unwrap(): Remove a Parent and Keep Its Children

Remove matching immediate parents while preserving their children, and understand layout, event, and restoration implications.

jQuery .removeClass(): Remove Specific or All Classes

jQuery .removeClass(): Remove Specific or All Classes

Remove selected class names or clear all classes, while distinguishing class removal from clearing inline styles.

jQuery .wrap(): Wrap Each Element Separately

jQuery .wrap(): Wrap Each Element Separately

Give each selected element its own wrapper, check the returned selection, and avoid duplicate IDs or repeated nesting.

jQuery .slideUp(): Collapse and Hide Content

jQuery .slideUp(): Collapse and Hide Content

Collapse a panel with duration, easing, and completion handling, including queued effects, reset behavior, and reduced motion.

jQuery :button Selector: Which Elements Does It Match?

jQuery :button Selector: Which Elements Does It Match?

Check how :button treats button elements, ordinary button inputs, and submit or reset inputs, with a working selection example.