Why we use createElement() instead of HTML?

I don’t understand why we use createElement() method in JS for creating div, li, p etc instead of using HTML code < div> etc, I mean to know the behind of reason of such method?

Using straight html is just fine for static content.
You would use js to create content when you need it to be dynamic or interactive, where the page content will vary dependant on certain conditions.


Very much as Sam has outlined. The best example I can think of would be when you have a variable length number of items stored in an array, or perhaps several database records. If you wanted to show these as a list or table, you could use createElement() as a way of generating the correct number of li or td elements.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.