diff --git a/files/en-us/_redirects.txt b/files/en-us/_redirects.txt index e270ff72c06515f..9145595fdf80591 100644 --- a/files/en-us/_redirects.txt +++ b/files/en-us/_redirects.txt @@ -7142,6 +7142,7 @@ /en-US/docs/Web/API/ChildNode.nextElementSibling /en-US/docs/Web/API/Element/nextElementSibling /en-US/docs/Web/API/ChildNode.remove /en-US/docs/Web/API/Element/remove /en-US/docs/Web/API/ChildNode/remove /en-US/docs/Web/API/Element/remove +/en-US/docs/Web/API/ChildNode/replaceWith /en-US/docs/Web/API/Element/replaceWith /en-US/docs/Web/API/Childnode.previousElementSibling /en-US/docs/Web/API/Element/previousElementSibling /en-US/docs/Web/API/Client/focus /en-US/docs/Web/API/WindowClient/focus /en-US/docs/Web/API/Client/focused /en-US/docs/Web/API/WindowClient/focused diff --git a/files/en-us/_wikihistory.json b/files/en-us/_wikihistory.json index 0595a9d44ef1955..1087e957373a150 100644 --- a/files/en-us/_wikihistory.json +++ b/files/en-us/_wikihistory.json @@ -34351,27 +34351,6 @@ "jpmedley" ] }, - "Web/API/ChildNode/replaceWith": { - "modified": "2020-10-15T21:47:15.895Z", - "contributors": [ - "mauricio-sg", - "stevenvachon", - "mfluehr", - "IlyaUpyackovich", - "Sephr", - "Peppesterest", - "zhuangyin", - "fscholz", - "subhaze", - "smukkekim", - "ght", - "ericyd", - "anonyco", - "stevenwdv", - "jszhou", - "jpmedley" - ] - }, "Web/API/Client": { "modified": "2020-10-15T21:31:16.211Z", "contributors": [ @@ -44281,6 +44260,27 @@ "krosylight" ] }, + "Web/API/Element/replaceWith": { + "modified": "2020-10-15T21:47:15.895Z", + "contributors": [ + "mauricio-sg", + "stevenvachon", + "mfluehr", + "IlyaUpyackovich", + "Sephr", + "Peppesterest", + "zhuangyin", + "fscholz", + "subhaze", + "smukkekim", + "ght", + "ericyd", + "anonyco", + "stevenwdv", + "jszhou", + "jpmedley" + ] + }, "Web/API/Element/requestFullScreen": { "modified": "2020-10-15T21:10:11.404Z", "contributors": [ diff --git a/files/en-us/web/api/characterdata/index.html b/files/en-us/web/api/characterdata/index.html index 58720d8fc701eb7..918ec577ea7743d 100644 --- a/files/en-us/web/api/characterdata/index.html +++ b/files/en-us/web/api/characterdata/index.html @@ -43,6 +43,8 @@
data
contains the modified {{domxref("DOMString")}}.CharacterData.data
of the specified length and starting at the specified offset.The CharacterData.replaceWith()
method replaces characters
+ in the children list of its parent with a set of {{domxref("Node")}} or {{domxref("DOMString")}} objects.
+ {{domxref("DOMString")}} objects are inserted as equivalent {{domxref("Text")}} nodes.
replaceWith(...nodes)+ +
nodes
replaceWith()
+<p id="myText">Some text</p> ++ +
let text = document.getElementById('myText').firstChild; +text.replaceWith("Other text"); ++ +
+<p id="myText">Other text</p> ++ +
{{Specifications}} + +
+{{Compat}}
+ +The ChildNode.replaceWith()
method replaces this
- ChildNode
in the children list of its parent with a set of
- {{domxref("Node")}} or {{domxref("DOMString")}} objects. {{domxref("DOMString")}}
- objects are inserted as equivalent {{domxref("Text")}} nodes.
[Throws, Unscopable] -void ChildNode.replaceWith((Node or DOMString)... nodes); -- -
nodes
replaceWith()
var parent = document.createElement("div"); -var child = document.createElement("p"); -parent.appendChild(child); -var span = document.createElement("span"); - -child.replaceWith(span); - -console.log(parent.outerHTML); -// "<div><span></span></div>" -- -
ChildNode.replaceWith()
is
- unscopableThe replaceWith()
method is not scoped into the with
- statement. See {{jsxref("Symbol.unscopables")}} for more information.
with(node) { - replaceWith("foo"); -} -// ReferenceError: replaceWith is not defined- -
You can polyfill the replaceWith()
method in Internet Explorer 10+ and
- higher with the following code:
function ReplaceWithPolyfill() { - 'use-strict'; // For safari, and IE > 10 - var parent = this.parentNode, i = arguments.length, currentNode; - if (!parent) return; - if (!i) // if there are no arguments - parent.removeChild(this); - while (i--) { // i-- decrements i and returns the value of i before the decrement - currentNode = arguments[i]; - if (typeof currentNode !== 'object'){ - currentNode = this.ownerDocument.createTextNode(currentNode); - } else if (currentNode.parentNode){ - currentNode.parentNode.removeChild(currentNode); - } - // the value of "i" below is after the decrement - if (!i) // if currentNode is the first argument (currentNode === arguments[0]) - parent.replaceChild(currentNode, this); - else // if currentNode isn't the first - parent.insertBefore(currentNode, this.nextSibling); - } -} -if (!Element.prototype.replaceWith) - Element.prototype.replaceWith = ReplaceWithPolyfill; -if (!CharacterData.prototype.replaceWith) - CharacterData.prototype.replaceWith = ReplaceWithPolyfill; -if (!DocumentType.prototype.replaceWith) - DocumentType.prototype.replaceWith = ReplaceWithPolyfill;- -
Specification | -Status | -Comment | -
---|---|---|
{{SpecName('DOM WHATWG', '#dom-childnode-replacewith', - 'ChildNode.replacewith()')}} | -{{Spec2('DOM WHATWG')}} | -Initial definition. | -
{{Compat("api.ChildNode.replaceWith")}}
- -The DocumentType.replaceWith()
method replaces the document type with a set of given nodes.
replaceWith(...nodes)+ +
nodes
replaceWith()
+let svg_dt = document.implementation.createDocumentType( + 'svg:svg', + '-//W3C//DTD SVG 1.1//EN', + 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd' +); + +document.doctype.replaceWith(svg_dt); ++ +
{{Specifications}} + +
+{{Compat}}
+ +The Element.replaceWith()
method replaces this
+ Element
in the children list of its parent with a set of
+ {{domxref("Node")}} or {{domxref("DOMString")}} objects. {{domxref("DOMString")}}
+ objects are inserted as equivalent {{domxref("Text")}} nodes.
replaceWith(...nodes)+ +
nodes
replaceWith()
const div = document.createElement("div"); +const p = document.createElement("p"); +div.appendChild(p); +const span = document.createElement("span"); + +p.replaceWith(span); + +console.log(div.outerHTML); +// "<div><span></span></div>" ++ +
replaceWith()
is
+ unscopableThe replaceWith()
method is not scoped into the with
+ statement. See {{jsxref("Symbol.unscopables")}} for more information.
with(node) { + replaceWith("foo"); +} +// ReferenceError: replaceWith is not defined+ +
{{Specifications}} + +
+{{Compat}}
+ +