Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Phaneendra N
Phaneendra N
1,218 Points

What is the best way to generate HTML from javascript?

Hi Everyone, I'm a back-end engineer. Started to learn javascript on treehouse recently. Dave McFarland is a great teacher. While it would take a lot of time for me to complete the javascript track, I'd like to get few ideas to solve the challenge am working on. I'd be very glad if the teacher answers this question. :) Challenge: I'm developing a graph traversal application. Which has a java rest API returning JSON data from bunch of endpoints. I'd like to write some javascript code to consume the the data, and generate markup corresponding to the data. e.g When a user selects an entity, it would call an end point and fetch the data related to the new entity, generates markup for that entity. An entity can be a person. Movie etc. I'd like this javascript code to be part of one single js file so that it can be included in any page I want. Within <head>. Google analytics js works this way? not quite sure.

I've referred this sample project on GitHub that in my understanding doing the similar stuff. include one javascript file, and it can sniff HTML string that a user submits, and produces the results of sniffing in a HTML table. https://github.com/squizlabs/HTML_CodeSniffer

How to go about writing this type functionality. Any ideas, suggestions or corrections are welcome :)

Thanks Phaneendra

1 Answer

Steven Parker
Steven Parker
231,248 Points

Your task is different, and simpler, than your example.

Generating HTML is much easier than analyzing it like the Code Sniffer does. You just need to convert your data into strings (if it's not already), wrap it with some HTML tags, and insert it into your document.

If you're just taking courses "a la carte", these two might be helpful for this particular task: AJAX Basics and JavaScript and the DOM.

Phaneendra N
Phaneendra N
1,218 Points

Thanks a lot, Steven Parker . I've actually started going through AJAX basics course before I saw your reply :) Awesome coincidence! Although I have no intention to complicate things, just want to know if there is a reason to try using node.js, angular etc for this particular task. (Am not very good at them either. Just worked out example application on angular website :) )

P.S: The idea is to make this functionality generic enough to plug into any client's web page.

Cheers!

Steven Parker
Steven Parker
231,248 Points

I wouldn't be concerned about node.js unless you were thinking of implementing this on the server. And Angular.js would add significantly to complexity but probably wouldn't be of much value to this task.

But I think jQuery would be quite handy here.

Phaneendra N
Phaneendra N
1,218 Points

Great advice. thanks again!