I have an index.html file with some handlebars template scripts (and the handlebars.js file in same directory..) I am just wondering why the handlebars script does not work if I place it under (outside) the tag... it only works inside it, why? Is it because the DOM is not already rendered at this point?
This works (but not if the part with "script id="quoteTemplate" ..... until document.getElementById(..))" is placed under the body... :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Handlebars Tutorial</title>
<style type="text/css">
.redText {color: red;}
.blueText {color: blue;}
.greenText {color: green;}
</style>
<script src="handlebars-v4.7.6.js"></script>
</head>
<body>
<h1>First handlebars tutorial...</h1>
<div id="marcData"></div>
<br>
<div id="quoteData"></div>
<script id="quoteTemplate" type="text/x-handlebars-template">
<h3>Favorite {{name}} Quotes</h3>
<ol>
{{#each quotes}}
<li>{{quote}}</li>
{{/each}}
</ol>
</script>
<script type="text/javascript">
let quoteInfo = document.getElementById("quoteTemplate").innerHTML;
let template = Handlebars.compile(quoteInfo);
let quoteData = template({
name: "Yogi Berra",
quotes: [
{ quote: "First quote" },
{ quote: "Second quote" },
{ quote: "Third quote" },
{ quote: "Fourth quote" },
{ quote: "Fifth quote" },
{ quote: "Sixth quote" }
]
});
document.getElementById("quoteData").innerHTML += quoteData;
</script>
</body>
<script type="text/javascript" src="src/js/first.js"></script>
</html>