I have a variable in my js file, let's say myCode = "<h1>HELLO</h1>";
myCode displays as <h1>HELLO</h1>
in HTML. . . I want it to render the h1 tag and just display HELLO
I have a variable in my js file, let's say myCode = "<h1>HELLO</h1>";
myCode displays as <h1>HELLO</h1>
in HTML. . . I want it to render the h1 tag and just display HELLO
LWC sanitizes what's rendered in the page to prevent security risk, that's why the default behavior is to display it as text instead of as a markup element.
Anyway, since some third-party libraries may need to manipulate the DOM, the framework exposes the lwc:dom="manual"
directive
HTML
<div class="myClass" lwc:dom="manual">
</div>
JS
modifyDom() {
const hello = '<h1>HELLO</h1>';
this.template.querySelector('.myClass').innerHTML = hello;
}
If you want to just render some text, you should leverage lightning-formatted-rich-text
to pass HTML tags in your component markup via its value
attribute.