Home > Mobile >  TypeError: (intermediate value).parseFromString(...).replace is not a function
TypeError: (intermediate value).parseFromString(...).replace is not a function

Time:12-16

I want to display the parsedHtml into element. but before display it, I should change a bit html into parsedHtml. so I used replace for this like code. but it has TypeError: (intermediate value).parseFromString(...).replace is not a function issue. Could you help me to fix this issue? Thanks

const parsedHTML = new DOMParser().parseFromString(html, "text/html");
const renderHtml = parsedHTML.replace('aria-expanded="false"', 'aria-expanded="true"');
console.log(renderHtml);

CodePudding user response:

You are treating a DOMParser object as a string. That will not work.

You need to do something like this since it is not recommended you change valid HTML before parsing it

const html = `<html><div aria-expanded="false"></div></html>`
const parsedHTML = new DOMParser().parseFromString(html, "text/html");
parsedHTML.querySelector("div").setAttribute("aria-expanded",true)
console.log(parsedHTML.querySelector("div"));

  • Related