Please help me add the html div tag in the script
const button = this.createElement("div");
button.classList.add("start_button");
let border = 0;
if (typeof this.config.backgroundImg === "string") {
button.classList.add("button");
border = 1;
}
button.innerText = (typeof this.config.startBtnName === 'string')
? this.config.startBtnName
: this.local("test");
this.config.startBtnName
is a text, i need this text to be inside <div>
I tried this '<div>'+this.config.startBtnName+'</div>'
, but then the div is output as plain text, not an html tag.
2
Answers
To insert HTML content, including a
div
tag, into a JavaScript-created element, you should use theinnerHTML
property instead ofinnerText
. TheinnerText
property treats the assigned value as plain text and does not parse it as HTML. However,innerHTML
will treat the assigned value as HTML, allowing you to insert tags likediv
.Here’s how you can modify your code to include the
div
tag:In this code,
innerHTML
is used to set the content of the button. Ifthis.config.startBtnName
is a string, it wraps that string in adiv
tag. Otherwise, it falls back to using the result ofthis.local("test")
, also wrapped in adiv
tag.Please ensure that
this.config.startBtnName
and the return value ofthis.local("test")
are properly sanitized if they can include user input, to avoid potential security risks such as Cross-Site Scripting (XSS).You could use
document.createElement
to make a div element, then add the text to it.