Reputation: 497
I have some HTML string. Use domparser to update some values, now i need back to HTML string format with updated values... Bcoz document.write accept only string.
Checkout the Sample patch,
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
// now i need to replace current update data entire screen
document.write(parsedHtml)
Thanks,
Gopal.R
Upvotes: 3
Views: 2197
Reputation: 1075537
Bcoz document.write accept only string.
Using document.write
is almost always poor practice.
But, if for some reason you really need to, what you get back from parseFromString
is a Document
object. It has a single documentElement
, which you can get the innerHTML
or outerHTML
of:
document.write(parsedHtml.documentElement.innerHTML);
// or
document.write(parsedHtml.documentElement.outerHTML);
Live Example:
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
// now i need to replace current update data entire screen
document.write(parsedHtml.documentElement.innerHTML);
But, again, probably better to just append to the page, e.g.
document.body.appendChild(parsedHtml.documentElement);
Live Example:
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
document.body.appendChild(parsedHtml.documentElement);
Or loop through it and append its children:
let child = parsedHtml.documentElement.firstChild;
while (child) {
let next = child.nextSibling;
document.documentElement.appendChild(child);
child = next;
}
Live Example:
const domName = 'MOBILE_NO';
// Below dom was getting from api.
const dom = '<html><head><title>Merchant Checkout Page</title></head><body><center><h1>Please do not refresh this page...</h1></center><form method="post" name="paytm_form"><input type="hidden" name="MOBILE_NO" value="xxxxxxxx"></form></body></html>';
const parser = new DOMParser();
const parsedHtml = parser.parseFromString(dom, 'text/html');
parsedHtml.getElementsByName(domName)[0].setAttribute('value', '1234567890');
let child = parsedHtml.documentElement.firstChild;
while (child) {
let next = child.nextSibling;
document.documentElement.appendChild(child);
child = next;
}
Upvotes: 3