--- title: ParentNode.append() slug: Web/API/ParentNode/append translation_of: Web/API/ParentNode/append ---
El método ParentNode.append()
inserta un conjunto de objetos de tipo {{domxref("Node")}} u objetos de tipo {{domxref("DOMString")}} después del último hijo de ParentNode
. Los objetos {{domxref("DOMString")}} son insertados como nodos {{domxref("Text")}} equivalentes.
Diferencias con {{domxref("Node.appendChild()")}}:
ParentNode.append()
te permite también agregar objetos {{domxref("DOMString")}}, mientras que Node.appendChild()
solo acepta objetos {{domxref("Node")}}.ParentNode.append()
no tiene valor de retorno, en cambio, Node.appendChild()
devuelve el objeto {{domxref("Node")}} agregado.ParentNode.append()
puede agregar varios nodos y cadenas, mientras que Node.appendChild()
sólo puede agregar uno.[Throws, Unscopable] void ParentNode.append((Node or DOMString)... nodes);
nodes
var parent = document.createElement("div"); var p = document.createElement("p"); parent.append(p); console.log(parent.childNodes); // NodeList [ <p> ]
var parent = document.createElement("div"); parent.append("Some text"); console.log(parent.textContent); // "Some text"
var parent = document.createElement("div"); var p = document.createElement("p"); parent.append("Some text", p); console.log(parent.childNodes); // NodeList [ #text "Some text", <p> ]
ParentNode.append()
is unscopableThe append()
method is not scoped into the with
statement. See {{jsxref("Symbol.unscopables")}} for more information.
var parent = document.createElement("div"); with(parent) { append("foo"); } // ReferenceError: append is not defined
You can polyfill the append() method
in Internet Explorer 9 and higher with the following code:
// Source: https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/append()/append().md (function (arr) { arr.forEach(function (item) { if (item.hasOwnProperty('append')) { return; } Object.defineProperty(item, 'append', { configurable: true, enumerable: true, writable: true, value: function append() { var argArr = Array.prototype.slice.call(arguments), docFrag = document.createDocumentFragment(); argArr.forEach(function (argItem) { var isNode = argItem instanceof Node; docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem))); }); this.appendChild(docFrag); } }); }); })([Element.prototype, Document.prototype, DocumentFragment.prototype]);
Specification | Status | Comment |
---|---|---|
{{SpecName('DOM WHATWG', '#dom-parentnode-append', 'ParentNode.append()')}} | {{Spec2('DOM WHATWG')}} | Initial definition. |
{{Compat("api.ParentNode.append")}}