Welcome to the Treehouse Community
Looking to learn something new?
melissa brown4,670 Points
what is the best way to add the js to the html elements?
for example do we add the h2 brackets like he has done in this code. if we have multiple sections for different html elements and css layout then is it best practice to add their location eg p, h2 in the jscript? what about when you start adding div etc then would we have to break up our js and add the src script into the html? what is the best practice so it doesnt get messy?
Vance Rivera18,322 Points
First and most importantly you will always want your js file added as the last to your body element. This way all of your Dom elements have been loaded and your js script has access to manipulate the Dom elements. Unless you are writing some arbitrary scripts you will want to stay away from writing script tags within your HTML tags as a placement. I never found a good reason to add a script tag within HTML tags as a placeholder. In order to manipulate the Dom and in your case insert text or other dynamic HTML markup you would want to use the concept of traversing the dom. Traversing is basically a way to navigate through the HTML elements based on their relations to other elements. With traversing you will use element selectors as well as CSS classes, id's, data tags, and other attributes to target specific elements. However before you can run you have to crawl so it will probably suite you best to work through the basic. I promise by the time you get through jquery you will be on your way manipulating the Dom the way you want. Hope this helps.
kenneth cootauco1,553 Points