Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between appendChild, insertAdjacentHTML, and innerHTML

Tags:

javascript

I want to know what the difference is between appendChild, insertAdjacentHTML, and innerHTML.

I think their functionality are similar but I want to understand clearly in term of usage and not the execution speed.

  1. For example, I can use innerHTML to insert a new tag or text into another tag in HTML but it replaces the current content in that tag instead of appends.
  2. If I would like to do it that way (not replace) I need to use insertAdjacentHTML and I can manage where I want to insert a new element (beforebegin, afterbegin, beforeend, afterend)

  3. And the last if I want to create (not insertion in current tag) a new tag and insert it into HTML I need to use appendChild.


Am I understanding it correctly? Or are there any difference between those three?

like image 257
Jongz Puangput Avatar asked Apr 21 '13 01:04

Jongz Puangput


People also ask

What is the difference between innerHTML and insertAdjacentHTML?

Using innerHTML means that any JavaScript references to the descendants of element will be removed. When you use insertAdjacentHTML , adding additional content will not corrupt the existing JS references and the existing nodes are not altered.

What is the difference between appendChild and innerHTML?

innerHTML is supposed to by assigned a HTML string, whereas appendChild() accepts only Node objects. When innerHTML property of an HTML element is assigned an HTML, the browser parses the string into nodes and then replaces the children of the parent element with the created nodes.

What is the difference between append and appendChild?

append() allows you to also append string objects, whereas Node. appendChild() only accepts Node objects. Element. append() has no return value, whereas Node.

What is difference between innerHTML and append () in JavaScript?

Answer : appendChild is used to insert new node in DOM. innerHTML is a property of DOM that allows to replace content of an element with different HTML, which automatically gets parsed into DOM nodes.


3 Answers

  • element.innerHTML

    From MDN:

    innerHTML sets or gets the HTML syntax describing the element's descendants.

    when writing to innerHTML, it will overwrite the content of the source element. That means the HTML has to be loaded and re-parsed. This is not very efficient especially when using inside loops.

  • node.appendChild

    From MDN:

    Adds a node to the end of the list of children of a specified parent node. If the node already exists it is removed from current parent node, then added to new parent node.

    This method is supported by all browsers and is a much cleaner way of inserting nodes, text, data, etc. into the DOM.

  • element.insertAdjacentHTML

    From MDN:

    parses the specified text as HTML or XML and inserts the resulting nodes into the DOM tree at a specified position. [ ... ]

    This method is also supported by all browsers.

....

like image 140
David G Avatar answered Nov 02 '22 20:11

David G


The appendChild methods adds an element to the DOM.

The innerHTML property and insertAdjacentHTML method takes a string instead of an element, so they have to parse the string and create elements from it, before they can be put into the DOM.

The innerHTML property can be used both for getting and setting the HTML code for the content of an element.

like image 23
Guffa Avatar answered Nov 02 '22 20:11

Guffa


@Guffa did explain the main difference ie innerHTML and insertAdjacentHTML need to parse the string before adding to DOM.

In addition see this jsPerf that will tell you that generally appendChild is faster for the job it provides.

like image 44
Arnaud Leyder Avatar answered Nov 02 '22 21:11

Arnaud Leyder