JavaScript Element insertAdjacentText() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance method

What You’ll Learn

Element.insertAdjacentText() is an instance method that inserts a plain text node at one of four positions: beforebegin, afterbegin, beforeend, and afterend. Learn the MDN demo pattern, comparison with insertAdjacentHTML(), safe user-input handling, and five try-it labs aligned with MDN.

01

Kind

Instance method

02

Returns

undefined

03

Args

where + data

04

Creates

Text node

05

Safe

plain text

06

Status

Baseline widely

Introduction

When you need to add words to the page — a username, a timestamp, or a chat message — you usually want plain text, not HTML markup. insertAdjacentText(where, data) creates a Text node from your string and places it at a precise position relative to an element.

MDN: given a relative position and a string, the method inserts a new text node at the given position relative to the element it is called from. The string is not parsed as HTML.

💡
Beginner tip

MDN recommends insertAdjacentText() or textContent when user-provided content should be plain text. Unlike insertAdjacentHTML(), angle brackets in the string are shown literally.

This page is part of JavaScript Element. See also insertAdjacentHTML() for parsed markup snippets.

Understanding the insertAdjacentText() Method

Calling target.insertAdjacentText(where, data) inserts a text node relative to target.

  • It is an instance method on Element.
  • First argument where: position string (MDN).
  • Second argument data: plain string for the new text node (MDN).
  • Returns undefined (MDN).
  • Does not parse HTML tags in data — they appear as literal text.
  • Creates a new text node; does not modify existing text nodes (MDN demo).
  • Same four positions as insertAdjacentHTML() and insertAdjacentElement().

📝 Syntax

General form of Element.insertAdjacentText (MDN):

JavaScript
insertAdjacentText(where, data)

Parameters

where — One of "beforebegin", "afterbegin", "beforeend", or "afterend" (MDN).

data — A string from which to create a new text node (MDN).

Return value

None (undefined) (MDN).

Position diagram (MDN)

JavaScript
<!-- beforebegin -->
<p>
  <!-- afterbegin -->
  foo
  <!-- beforeend -->
</p>
<!-- afterend -->

Common patterns

JavaScript
// MDN demo pattern
beforeBtn.addEventListener("click", () => {
  para.insertAdjacentText("afterbegin", textInput.value);
});

afterBtn.addEventListener("click", () => {
  para.insertAdjacentText("beforeend", textInput.value);
});

// Safe user comment
box.insertAdjacentText("beforeend", userComment);

// Label before an input
field.insertAdjacentText("beforebegin", "Email: ");

⚡ Quick Reference

GoalCode
Insert text inside endel.insertAdjacentText("beforeend", data)
Insert text inside startel.insertAdjacentText("afterbegin", data)
Before elementel.insertAdjacentText("beforebegin", data)
After elementel.insertAdjacentText("afterend", data)
Return valueundefined
MDN statusBaseline Widely available (since April 2018)

🔍 At a Glance

Four facts to remember about Element.insertAdjacentText().

Returns
void

undefined

Baseline
widely

Since Apr 2018

Args
where

+ data

Node
Text

Plain

📋 Text insertion APIs

insertAdjacentText()insertAdjacentHTML()textContent
InputPlain stringHTML string (parsed)Plain string (replaces)
Positions4 relative slots4 relative slotsWhole element only
CreatesText nodeParsed nodesSingle text child
User inputSafe for plain text (MDN)XSS risk (MDN)Safe for plain text
Best forAdjacent plain textHTML snippetsReplace all text

Examples Gallery

Examples follow MDN Element.insertAdjacentText() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

MDN insert before/after paragraph text.

Example 1 — MDN Insert Before / After

Add text inside a paragraph at the start or end (MDN demo).

JavaScript
const para = document.getElementById("para");
const text = document.getElementById("textInput").value;

beforeBtn.addEventListener("click", () => {
  para.insertAdjacentText("afterbegin", text);
});

afterBtn.addEventListener("click", () => {
  para.insertAdjacentText("beforeend", text);
});
Try It Yourself

How It Works

MDN’s demo adds further text nodes without replacing the paragraph’s existing text. Each click creates a new Text node.

Example 2 — afterbegin Prefix

Prepend plain text inside an element before its children.

JavaScript
const label = document.getElementById("label");

label.insertAdjacentText("afterbegin", "Note: ");
console.log(label.textContent); // "Note: Original label"
Try It Yourself

How It Works

afterbegin inserts a text node as the first child inside the element, before any existing content (MDN).

📈 Practical Patterns

Append text, sibling labels, and HTML comparison.

Example 3 — beforeend User Comment

Append a safe plain-text comment from user input.

JavaScript
const thread = document.getElementById("thread");
const comment = getUserInput(); // plain text

thread.insertAdjacentText("beforeend", comment);
console.log("comment added");
Try It Yourself

How It Works

MDN recommends insertAdjacentText() when user content should stay plain text. Tags like <b> are shown literally, not parsed.

Example 4 — beforebegin Field Label

Insert a text label immediately before an input element.

JavaScript
const email = document.getElementById("email");

email.insertAdjacentText("beforebegin", "Email: ");
console.log(email.previousSibling.textContent); // "Email: "
Try It Yourself

How It Works

beforebegin adds a sibling text node before the target element. MDN: requires an element parent in the DOM tree.

Example 5 — vs insertAdjacentHTML()

Same string behaves differently — text is not parsed as markup.

JavaScript
const box = document.getElementById("box");
const raw = "<strong>bold</strong>";

box.insertAdjacentText("beforeend", raw);
console.log(box.textContent);
// "<strong>bold</strong>" shown literally

// insertAdjacentHTML would create a <strong> element instead
Try It Yourself

How It Works

insertAdjacentText() never parses HTML. Use insertAdjacentHTML() only when you intentionally want markup.

🚀 Common Use Cases

  • Adding user comments safely without HTML parsing (MDN).
  • Prefixing labels with afterbegin or beforebegin.
  • Appending timestamps or status text with beforeend.
  • Building chat UIs where messages are plain text.
  • Inserting counters or separators next to elements.
  • Avoiding XSS when displaying untrusted strings as text.

🧠 How insertAdjacentText() Inserts Text

1

Choose where + data

target.insertAdjacentText("beforeend", data)

Args
2

Create Text node

Browser creates a new text node from data — not parsed as HTML (MDN).

Text
3

Insert at position

Text node is placed relative to the target at the chosen slot (MDN).

DOM
4

Done — returns undefined

Existing text nodes are unchanged; new text is added alongside them (MDN demo).

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, April 2018).
  • Returns undefined (MDN).
  • Does not parse HTML in data — angle brackets appear as literal text.
  • MDN: safe choice for user-provided plain text vs insertAdjacentHTML().
  • beforebegin / afterend require an element parent in the tree (MDN).
  • Related: insertAdjacentHTML(), insertAdjacentElement(), append(), JavaScript hub.

Browser Support

Element.insertAdjacentText() is Baseline Widely available (MDN: across browsers since April 2018). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.insertAdjacentText()

Insert plain text at beforebegin, afterbegin, beforeend, or afterend — returns undefined.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer Not supported
No
insertAdjacentText() Excellent

Bottom line: Use insertAdjacentText() for safe plain-text insertion. Use insertAdjacentHTML() only when you need parsed markup.

Conclusion

Element.insertAdjacentText() inserts a plain text node at one of four positions around a target element. It is the safe, beginner-friendly choice when you need words — not HTML markup — in a precise spot.

Continue with insertAdjacentHTML(), insertAdjacentElement(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use for user comments, labels, and timestamps (MDN)
  • Pick the right slot with the MDN position diagram
  • Prefer this over insertAdjacentHTML() for untrusted strings
  • Use afterbegin / beforeend inside containers
  • Combine with form input values for live text updates

❌ Don’t

  • Expect HTML tags to render (use insertAdjacentHTML instead)
  • Expect a return reference to the new text node
  • Use invalid where strings (throws per MDN)
  • Confuse with textContent (which replaces all text)
  • Use beforebegin on detached elements without a parent

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.insertAdjacentText()

Plain text at four positions.

5
Core concepts
📄 02

Input

plain

Text
📄 03

Safe

user

MDN
04

Slots

4 pos

Same
05

vs

HTML

Parse

❓ Frequently Asked Questions

It inserts a new text node at a position relative to the element (MDN). You pass a position string and plain text — the string is not parsed as HTML.
No. MDN marks Element.insertAdjacentText() as Baseline Widely available (across browsers since April 2018). It is not Deprecated, Experimental, or Non-standard.
Nothing useful — the return value is undefined (MDN).
insertAdjacentText() creates a Text node from a plain string. insertAdjacentHTML() parses HTML markup, which can be unsafe with user input (MDN).
beforebegin, afterbegin, beforeend, and afterend — the same four slots as insertAdjacentHTML() and insertAdjacentElement() (MDN).
MDN recommends it when user-provided content should be plain text, not HTML — for example comments, labels, or chat messages.
Did you know?

MDN: the existing text node inside an element is not replaced — further insertAdjacentText() calls create additional text nodes alongside it.

More Element Topics

Browse Element methods and properties to keep building your DOM skills.

matches() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful