When should JavaScript generate HTML?
Posted
by
VirtuosiMedia
on Programmers
See other posts from Programmers
or by VirtuosiMedia
Published on 2012-03-28T21:42:37Z
Indexed on
2012/03/28
23:42 UTC
Read the original article
Hit count: 717
JavaScript
|html
I try to generate as little HTML from JavaScript as possible. Instead, I prefer to manipulate existing markup whenever I can and only generate HTML when I need to dynamically insert an element that isn't a good candidate for using Ajax. This, I believe, makes it far easier to maintain the code and quickly make changes to it because the markup is easier to read and trace. My rule of thumb is: HTML is for document structure, CSS is for presentation, JavaScript is for behavior.
However, I've seen a lot of JS code that generates mounds of HTML, including entire forms and content-heavy modal dialogs. In general, which method is considered best practice? In what circumstances should JavaScript be used to generate HTML and when should it not?
© Programmers or respective owner