jQuery .insertAfter(): Insert Content as the Next Sibling
jQuery .insertAfter() inserts the selected content immediately after each target. The new element and its target share a parent. Use it when a message belongs beside an existing element in the document tree, such as an error paragraph that follows a form field.
Insert a sibling immediately after a target
$(content).insertAfter(target);
Select or create the content first. Pass the destination selector, DOM element, or jQuery object as target. The Document Object Model (DOM) represents the document as a tree of nodes. The returned collection contains the inserted elements, so a chained .addClass() styles those elements.
Official jQuery .insertAfter() documentation
Show an error message after an input
Save the code as insert-after-example.html in UTF-8 and open it in a browser. It loads jQuery 4.0.0 from a CDN and requires internet access. Use the form to check the format of an email address.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Insert an error after a form field</title>
<style>
body { margin: 1rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
main { max-width: 32rem; margin-inline: auto; }
label { display: block; }
input, button { font: inherit; padding: .35rem .6rem; }
input { box-sizing: border-box; width: min(100%, 24rem); }
.error { color: #8e1c11; background: #fff0eb; padding: .35rem .5rem; }
</style>
<script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
<main>
<h1>Insert an error after a form field</h1>
<form id="form" novalidate>
<div class="field">
<label for="email">Email address</label>
<input id="email" name="email" type="email" required autocomplete="email" aria-describedby="email-help">
<p id="email-help">Example: name@example.com</p>
</div>
<button type="submit">Check format</button>
</form>
<p id="status" role="status"></p>
</main>
<script>
$(function () {
const $email = $("#email");
$("#form").on("submit", function (event) {
event.preventDefault();
$("#email-error").remove();
$email.removeAttr("aria-invalid").attr("aria-describedby", "email-help");
$("#status").text("");
if (!$email[0].validity.valid) {
$("<p>").attr("id", "email-error").addClass("error")
.text("Enter an email address such as name@example.com.").insertAfter($email);
$email.attr({ "aria-invalid": "true", "aria-describedby": "email-error email-help" }).trigger("focus");
return;
}
$("#status").text("The email address format is valid.");
});
});
</script>
</body>
</html>
- Leave the input empty or enter
bad-addressand submit. The error appears immediately after the field. The original hint remains below it. - Submitting the same value again leaves one error paragraph. The code removes the previous error and uses
aria-describedbyto associate the new error and the original hint with the field. - Change the value to
name@example.comand submit again. The error andaria-invalidare cleared and a success message appears.
The example uses validity.valid to read the result of the browser’s required and email-format checks. novalidate suppresses the default validation popup on submit. The example checks the format locally without submitting the address or checking whether the mailbox exists.
HTML Standard email input and validity rules
Move an existing node or insert it in several places
A single target receives the original when you insert an existing node. Run this code in the example’s console. It prints 0 true, then true. The original parent is empty and the target’s next element sibling is the original node.
const $source = $("<div>");
const $targetParent = $("<div>");
const $item = $("<p>").text("Paragraph to move").appendTo($source);
const $target = $("<p>").text("Target paragraph").appendTo($targetParent);
const $inserted = $item.insertAfter($target);
console.log($source.children().length, $inserted[0] === $item[0]);
console.log($target[0].nextElementSibling === $item[0]);
With multiple targets, the last receives the original and preceding targets receive copies. The returned collection includes them all. Use a fixed ID such as email-error for a single field or assign a unique ID to each inserted message.
Compare insertion positions and return values
| Method | Insertion position | Returned elements |
|---|---|---|
.insertAfter() |
Immediately after the target, under the same parent | Inserted elements |
.after() |
Immediately after the target, under the same parent | Target elements |
.insertBefore() |
Immediately before the target, under the same parent | Inserted elements |
.appendTo() |
At the end inside the target | Inserted elements |
$(target).after(content) and $(content).insertAfter(target) insert in the same position. Chain after .after() to act on the target. Use the result of .insertAfter() to act on the inserted elements.
Official jQuery .after() documentation
Insert text and troubleshoot the result
To display a string from a form or API, create an empty element and set it with .text(). Run this snippet in the example’s console to add <Extra hint> after the original hint with its angle brackets visible. Reloading clears the added paragraph.
const message = "<Extra hint>";
$("<p>").text(message).insertAfter("#email-help");
- If the insertion is missing, check the selector’s
.lengthand whether the target is attached to a parent. - In the Elements panel, check that the error is the next element after
#email. CSS can rearrange what you see, so inspect the DOM relationship. - If repeated actions pile up messages, remove the previous message by its own selector as in this example or update its text.
Insert inside a destination with .appendTo() · Start with the destination in .after() · Insert before the target with .insertBefore()




