I volontari di MDN non hanno ancora tradotto questo articolo in Italiano. Unisciti a noi e traducilo tu stesso.
Puoi anche consultare l’articolo in English (US).

The insertAdjacentText() method of the Element interface inserts a given text node at a given position relative to the element it is invoked upon.


element.insertAdjacentText(position, element);


A DOMString representing the position relative to the element; must be one of the following strings:
  • 'beforebegin': Before the element itself.
  • 'afterbegin': Just inside the element, before its first child.
  • 'beforeend': Just inside the element, after its last child.
  • 'afterend': After the element itself.
A DOMString representing the text to be inserted into the tree.

Return value



Exception Explanation
SyntaxError The position specified is not a recognised value.

Visualization of position names

<!-- beforebegin -->
  <!-- afterbegin -->
  <!-- beforeend -->
<!-- afterend -->
Note: The beforebegin and afterend positions work only if the node is in a tree and has an element parent.


beforeBtn.addEventListener('click', function() {

afterBtn.addEventListener('click', function() {

Have a look at our 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.


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){
      (txt+'') // convert to string
        .replace(/&/g, '&amp;') // embed ampersand symbols
        .replace(/</g, '&lt;') // embed less-than symbols


Specification Status Comment
The definition of 'insertAdjacentText()' in that specification.
Living Standard  

Browser compatibility

Update compatibility data on GitHub
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
insertAdjacentTextChrome Full support 1Edge Full support 17Firefox Full support 48IE Full support YesOpera Full support YesSafari Full support 10WebView Android Full support 2.3Chrome Android Full support 18Edge Mobile Full support 12Firefox Android Full support 48Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes


Full support  
Full support

See also

Tag del documento e collaboratori

Hanno collaborato alla realizzazione di questa pagina: mdnwebdocs-bot, mfluehr, fscholz, sideshowbarker, NemoNobobyPersonne, anonyco, Rob W, Marat-Tanalin, vsemozhetbyt, chrisdavidmills
Ultima modifica di: mdnwebdocs-bot,