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 trialGabriel Kaunda
12,170 PointsHow to include HTML elements
Dave wrote is code like this:
var sentence = "<h2>There was once a" + adjective; sentence += ' programmer who wanted to use Javascript to ' + verb; sentence += 'the ' + noun '.</h2>' document.write(sentence)
Is there anything wrong with writing it this way?
var sentence = "<h2>There was once a" + adjective + ' programmer who wanted to use Javascript to ' + verb + 'the ' + noun '.</h2>' document.write(sentence)
Also when you include HTML elements in Javascript do you always have to put them in qoute marks? if yes then how do you write an HTML element if you want it to appear on your page inside a string e.g "This is how to close an html element tag </h2>, </p>, but some do not require closing like the <img/> element which are self closing."
1 Answer
Ari Misha
19,323 PointsHiya Gabriel! There is nothing wrong with your way of adding stuff to a "document"( as in HTML document tree). But sometimes you need to divide a really long string into in different parts coz its considered a best practice as a programmer. And it makes easily readable for human eyes. And regarding self closing tags, you can add self-closing tags as well and JavaScript is smart enough to know that it is one long string that needs to be added to the DOM tree and DOM knows its a node tag. I hope it helped. (:
Gabriel Kaunda
12,170 PointsGabriel Kaunda
12,170 PointsThank you, that really helped