0

Please ignore my syntax. This is just an example if this is a bad idea.

let's say I have select with 50 options but instead of typing out options 50 times.

would it be a good idea to use js and give it an array variable. run through the variable.length then append it into html.

Is it bad to use js like this though and why?

Tsuna
  • 2,098
  • 6
  • 24
  • 46
  • 1
    Refer http://stackoverflow.com/questions/2839844/best-practice-for-inserting-large-chunks-of-html-into-elements-with-javascript http://stackoverflow.com/questions/220603/is-there-a-best-practice-for-generating-html-with-javascript – Anurag Sinha Dec 23 '16 at 07:12
  • That depends on the situation. For instance, typing out your HTML would allow caching, which is good for static options, whereas the JavaScript solution would allow you to cache anything BUT the options, which would be good for dynamic or changing options. – Emil S. Jørgensen Dec 23 '16 at 07:13
  • No, look at angular and SPA's. You can use jQuery, though I recommend looking into angular if you plan to do a lot of client-side rendering. – Philip Rollins Dec 23 '16 at 07:13
  • have a look at http://softwareengineering.stackexchange.com/questions/142002/when-should-javascript-generate-html – Naghaveer R Dec 23 '16 at 07:15

2 Answers2

2

No issues. Now clients are way too much powerful than they were earlier before. So, utilizing some of its power never harms.

Now the problem .. rendering template on client is not an issue ..Angular, React all does the same.

While you are doing only a little which may consumes less than < 1 ms. So , just go ahead.

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
0

it is a better practice. You are a programmer, you will never need to type out options 50 times :)

kchr
  • 1
  • 3
  • though it's much easier to do, you should take the speed of your website into account, and security even more, sometimes, using JS to append elements to the page can a security flaw in one way or another. – Grey Dec 23 '16 at 07:31