List the disadvantages of using innerHTML in javascript.
Ans: The innerHTML property is a part of the DOM and is used to set or return the HTML content of an element. The return value represents the text content of the HTML element. It allows JavaScript code to make changes to a website being rendered.
The disadvantages of using innerHTML are:
- Appending to innerHTML is not supported without reparsing the whole innerHTML.
- As reparsing is required for innerHTML the processing is slow and takes more time.
- The event handlers do not attach automatically to the newly created elements by setting innerHTML. One must keep track of the event handler and attach the new element manually.
- By using innerHTML if you add, append, delete or modify contents on a webpage all contents are replaced, also all the DOM nodes inside that element are reparsed and recreated.
- No proper validation is provided by innerHTML, hence any valid HTML code can be used. This may break the document of JavaScript.
