Possible Duplicate:
JavaScript: Is it better to use innerHTML or (lots of) createElement calls to add a complex div structure?
What should I use instead of .innerHTML
? All advice is appreciated.
innerHTML = template; And if you want a more React/JSX-like approach, you can use template literals instead. Note: only works in modern browsers—you'd need to use Babel to transpile it. var app = document.
The use of innerHTML creates a potential security risk for your website. Malicious users can use cross-site scripting (XSS) to add malicious client-side scripts that steal private user information stored in session cookies.
innerText returns all text contained by an element and all its child elements. innerHtml returns all text, including html tags, that is contained by an element.
There is no append support without reparsing the whole innerHTML. This makes changing innerHTML directly very slow. innerHTML does not provide validation and therefore we can potentially insert valid and broken HTML in the document and break it.
The correct answer is that in certain situations you should use innerHTML, and in other situations you should use appendChild. Here's when to use innerHTML or appendChild:
Use innerHTML when you're setting text inside of an HTML tag like an anchor tag, paragraph tag, span, div, or textarea.
Use appendChild() If you're trying to add new DOM elements inside of another DOM element.
You can use W3C compliant methods, like:
createElement()
[docs] appendChild()
[docs] removeChild()
[docs] replaceChild()
[docs] insertBefore()
[docs] createTextNode()
[docs] If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With