Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a way to do it for Firefox, Opera, Chrome and Safari. Basically, you just do div.innerHTML = div.innerHTML to reinterpret its content as HTML, which will make that class attribute from the XML file be treated as an HTML class name.</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang="en"&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;&lt;/title&gt; &lt;script&gt; window.addEventListener("DOMContentLoaded", function() { var div = document.getElementsByTagName("div")[0]; var req = new XMLHttpRequest(); req.onreadystatechange = function() { if (this.readyState === 4 &amp;&amp; this.status === 200) { var doc = this.responseXML; div.appendChild(document.importNode(doc.getElementsByTagName("response")[0].getElementsByTagName("div")[0], true)); div.innerHTML = div.innerHTML; alert(document.getElementsByClassName("colorSelector").length); } }; req.open("GET", "div.xml"); req.send(); }, false); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="testA"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Remove the this.status === 200 if you're testing locally in browsers that support xhr locally.</p> <p>The importNode() function doesn't seem to work in IE (9 for example). I get a vague "interface not supported" error.</p> <p>You could also do it this way:</p> <pre><code>var doc = this.responseXML; var markup = (new XMLSerializer()).serializeToString(doc.getElementsByTagName("response")[0].getElementsByTagName("div")[0]); div.innerHTML = markup; </code></pre> <p>as long as the markup is HTML-friendly as far as end tags for empty elements are concerned.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload