The insertAdjacentText() method of the Element interface inserts a given text node at a given position relative to the element it is invoked upon.
Syntax
element.insertAdjacentText(position, element);
Parameters
position- A
DOMStringrepresenting the position relative to theelement; must be one of the following strings:
'beforebegin'- Before the
elementitself. 'afterbegin': Just inside theelement, before its first child.'beforeend': Just inside theelement, after its last child.'afterend': After theelementitself.
- Before the
element- A
DOMStringrepresenting the text to be inserted into the tree.
Return value
Void.
Exceptions
| Exception | Explanation |
|---|---|
SyntaxError
|
The position specified is not a recognised value.
|
Visualization of position names
<!-- beforebegin --> <p> <!-- afterbegin --> foo <!-- beforeend --> </p> <!-- afterend -->
Note: The beforebegin and afterend positions work only if the node is in a tree and has an element parent.
Example
beforeBtn.addEventListener('click', function() {
para.insertAdjacentText('afterbegin',textInput.value);
});
afterBtn.addEventListener('click', function() {
para.insertAdjacentText('beforeend',textInput.value);
});
Have a look at our [[../../../../../../../mdn.github.io/dom-examples/insert-adjacent/insertAdjacentText|insertAdjacentText.html]] demo on GitHub (see the source code too.) Here we have a simple paragraph. You can enter some text into the form element, then press the Insert before and Insert after buttons to insert it before or after the existing paragraph text using insertAdjacentText(). Note that the existing text node is not added to — further text nodes are created containing the new additions.
Polyfill
You can polyfill the insertAdjacentText() method in Internet Explorer 5.5 (maybe earlier) and higher with the following code:
if (!Element.prototype.insertAdjacentText)
Element.prototype.insertAdjacentText = function(type, txt){
this.insertAdjacentHTML(
type,
(txt+'') // convert to string
.replace(/&/g, '&') // embed ampersand symbols
.replace(/</g, '<') // embed less-than symbols
)
}
Specification
| Specification | Status | Comment |
|---|---|---|
| DOMThe definition of 'insertAdjacentText()' in that specification. | Living Standard |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
insertAdjacentText
|
Chrome
Full support 1 |
Edge Full support 17 Full support 17 No support 12 — 17 Notes' This function is implemented in the |
Firefox
Full support 48 |
IE Full support Yes Full support Yes Notes' This function is implemented in the |
Opera
Full support Yes |
Safari
Full support 4 |
WebView Android
Full support 2.3 |
Chrome Android
Full support 18 |
Firefox Android
Full support 48 |
Opera Android
Full support Yes |
Safari iOS
Full support 4 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
- See implementation notes.'
- See implementation notes.
See also
Element.insertAdjacentText() by Mozilla Contributors is licensed under CC-BY-SA 2.5.