jQuery .prependTo(): Insert Content as the First Child
jQuery .prependTo() puts the selected content at the start inside each destination. With one destination, an existing node moves there. To keep the original in place and show it in other containers, pass a clone.
Start with the content to insert
$(content).prependTo(target);
Select or create the content before calling the method. Pass a selector, a DOM element, or a jQuery object as target. The Document Object Model (DOM) represents a document as a tree of nodes. The returned collection contains the inserted elements, so a chained .addClass() acts on those elements.
Official jQuery .prependTo() documentation
Move or copy a notice in a runnable example
Save the code as prepend-to-example.html in UTF-8 and open it in a browser. It loads jQuery 4.0.0 from a CDN and requires internet access. Each action resets the example first so you can compare moving and copying under the same conditions.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Insert a notice at the start of a panel</title>
<style>
body { margin: 1rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
main { max-width: 42rem; margin-inline: auto; }
.columns { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fit, minmax(min(100%,14rem),1fr)); }
section { padding: .75rem 1rem; border: 1px solid #a5b3c0; border-radius: .5rem; }
section h2 { margin-top: 0; font-size: 1.1rem; }
button { font: inherit; margin: .25rem; padding: .35rem .6rem; }
.highlight { background: #ffe8a3; color: #4b3208; }
</style>
<script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
<main>
<h1>Insert a notice at the start of a panel</h1>
<section>
<h2>Original</h2>
<div id="source"><p id="notice">Weekend opening notice</p></div>
</section>
<button type="button" id="move">Move to the first panel</button>
<button type="button" id="copy">Copy to both panels</button>
<button type="button" id="reset">Reset</button>
<div class="columns">
<section><h2>First panel</h2><div id="alpha" class="target"><p>Existing content in the first panel</p></div></section>
<section><h2>Second panel</h2><div id="beta" class="target"><p>Existing content in the second panel</p></div></section>
</div>
</main>
<script>
$(function () {
const $notice = $("#notice");
let $copies = $();
function reset() {
$copies.remove();
$copies = $();
$notice.prependTo("#source").removeClass("highlight");
}
$("#move").on("click", function () {
reset();
$notice.prependTo("#alpha").addClass("highlight");
});
$("#copy").on("click", function () {
reset();
$copies = $notice.clone().removeAttr("id")
.prependTo(".target").addClass("highlight");
});
$("#reset").on("click", reset);
});
</script>
</body>
</html>
- Click Move to the first panel. The source becomes empty and the notice appears above the first panel’s existing content. The second panel shows its original content.
- Click Copy to both panels. The notice appears at its original location, and a highlighted copy appears at the top of each destination.
- Click Reset to remove the copies and restore the original. In the Elements panel, confirm that only the original has
id="notice".
Handle IDs on copies and insert new text
With multiple destinations, .prependTo() places the supplied node in the last destination and copies in the earlier ones. The Copy button first creates a separate node with .clone().removeAttr('id'). If the original also has descendants with IDs, make those values unique in the document as well.
To insert a new message, run this snippet in the example page’s console. It creates a paragraph, sets its text with .text(), and puts it inside the first panel.
const incoming = "<Extra notice>";
$("<p>").text(incoming).prependTo("#alpha").addClass("highlight");
<Extra notice> appears at the top of the first panel with the angle brackets visible. Use the same approach for text from a form or API. Reload the page to clear the separate item inserted by this console snippet.
How jQuery .text() displays strings
Tell a first child from a preceding sibling
| Method | Position of the new element | Returned elements |
|---|---|---|
.prependTo() |
Beginning inside the destination | Inserted elements |
.prepend() |
Beginning inside the destination | Destination elements |
.appendTo() |
End inside the destination | Inserted elements |
.insertBefore() |
Immediately before the destination, under the same parent | Inserted elements |
A paragraph inserted by .prependTo() has the destination as its parent. A paragraph inserted by .insertBefore() shares a parent with the destination. Run the following in the example console to confirm both relationships.
const $wrapper = $("<div>");
const $target = $("<section>").appendTo($wrapper);
const $child = $("<p>").text("First child").prependTo($target);
const $sibling = $("<p>").text("Preceding sibling").insertBefore($target);
console.log($target[0].firstElementChild === $child[0]);
console.log($sibling[0].nextElementSibling === $target[0]);
The first true confirms that $child is the destination’s first element child. The second confirms that the destination immediately follows $sibling. These temporary elements stay outside the visible page.
Official jQuery .insertBefore() documentation
Troubleshoot unexpected insertion results
- If the notice does not appear, check the
.lengthof both selections and whether the code runs after the document is ready. - If the original disappeared from its old position, inspect its new parent. Pass a clone to keep the original in place.
- If you get more inserted elements than expected, count how many destinations match the selector.
Select the destination first with .prepend() · Insert at the end with .appendTo()




