One thing to remember is that not everyone has JavaScript. JavaScript should only be used to add features to a web page that make it easier to use - not to provide the basic functioning that the page is there for.

If you want to generate that code instead of having to have it all hard coded then generate it using a server side language. That way you have the benefit of it being program generated AND everyone including those without JavaScript still sees the same HTML.