Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Manipulating Elements

— So we've created an element, what do we do with it now? — Let's add it to the page — every element deserves its own DOM.

It's time to learn how to add elements to a page. All the methods we need are gathered in one section of the documentation — Manipulation. We've already met some of them, and there's just a little bit left:

inserts content after each element in the selection, i.e. if you see the line $('p').after('<hr/>'), read it as "after each paragraph a line will be added"

inserts elements from the selection after each element passed as an argument, i.e. if you see the line $('<hr/>').insertAfter('p'), read it as "a line will be inserted after each paragraph"

— Hmm, I don't see the difference!

— It's easy, look closer:

$("found element").after("what to add after it?")
$("what to add").insertAfter("after which element?")

Let's repeat. Take some arbitrary text and add it after each paragraph:

$('p').after('(^_^)ノ')

If we want to add a new DOM element, we can pass an HTML string as the parameter and hope that jQuery figures out what to do with it:

$('p').after('<hr/>')

If we want to move a DOM element, we'll need to explicitly "select" it using a jQuery call:

$('p').after($('h1'))

If we don't do that, we'll get a completely different result:

$('p').after('h1')

Good, we've covered the after() method — next up is the before() method:

inserts content before each selected element

inserts elements from the selection before each element passed as an argument

— OK, I think I'm starting to get it:


The following methods take the target elements and modify them:

inserts content at the end of each element in the selection, i.e. the line $("p").append("<hr/>") should be read as "a line will be added to the end of each paragraph"

inserts the selected content at the end of each element passed as an argument; $("<hr/>").appendTo("p") — "a line will be added to the end of each paragraph"

— Again about the difference:

inserts content at the beginning of each element in the selection

inserts the selected content at the beginning of each element passed as an argument


Alright, we've sorted out this chunk of documentation. Again, feel the difference between the listed methods, because there are more coming:

replaces the found elements with a new one: $("p").replaceWith("<hr/>")

inserts content in place of the found elements: $("<hr/>").replaceAll("h3")

— Ah, right:


wraps each found element with a new element; think of it as taking candies from a box and wrapping each one

wraps the found elements with a new element; we take a bunch of candies and wrap them in one big wrapper

wraps the content of each found element with a new element; take the candies, unwrap each one, wrap in a new wrapper, then put the original wrapper back on top

removes the parent element of the found elements; wrappers off!

Sure, I could pretend to be a smarty-pants here, but in reality I'm constantly peeking at method names too when working with the DOM.


Here's a homework assignment for you — write a script that adds the following HTML to the page:

Last updated