Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Get and Set Content with textContent and innerHTML 8:00
- Change Element Attributes 5:50
- Modifying Elements 2 objectives
- Set Inline Styles with the style Property 9:38
- Create New DOM Elements 4:35
- Review Attributes and Element Creation 5 questions
- Append Nodes 4:52
- DOM Manipulation Review 3 questions
- Insert HTML at Specified Positions 3:39
- Remove Nodes 4:20
- Review Appending and Removing Nodes 5 questions

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Use the InsertAdjacentHTML
method to insert elements into the DOM. This method provides more control over inserting HTML inside a parent. You pass it the HTML and text content to create as a string, and then that string gets parsed and inserted into the DOM at the position you specify.
Further Reading
Position arguments
insertAdjacentHTML()
accepts one of the following strings representing the position relative to an element:
-
'beforebegin'
: Before theelement
itself. -
'afterbegin'
: Just inside theelement
, before its first child. -
'beforeend'
: Just inside theelement
, after its last child. -
'afterend'
: After theelement
itself.
Some additional thoughts:
It's worth mentioning that there are some security considerations that should be taken when inserting HTML into a page with insertAdjacentHTML()
and other similar methods.
Cross-site scripting (XSS) is a security exploit that allows an attacker to inject malicious client-side code into a website. This can occur when insertAdjacentHTML()
is used to insert plain text instead of raw text, which can then be executed as a script within the client's browser. For this reason, it is important to think about where the data is being inserted from. If data is being inserted from a trustworthy backend, then insertAdjactentHTML()
offers a fast and reliable approach to inserting content into the DOM.
If your data source is unknown, you may want to consider using Node.textContent or Element.insertAdjacentText(), as these will insert the content as raw text instead of HTML.
To learn more, check out Treehouse's dedicated Web Security course. You may also want to read more in the security sections of the following MDN articles:
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Nataly Mota
5,295 PointsI meant to click on :-) at the end of the second to last tutorial video in JS and the DOM, but accidentally clicked :-(.
Posted by Nataly MotaNataly Mota
5,295 Points2 Answers
-
Malachi Gruenhagen
3,226 Points1 Answer
View all discussions for this video
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up