mardi 25 septembre 2018

js code does not work in html as a separate file (no jQuery)

I have a really simple js function (I'm not using jQuery) that works in html using the <script> tag, but it doesn't work when I put it in a separate .js file. I know I'm doing something wrong and it's probably just a simple fix, but I can't figure it out. Any help? Thank you very much!

Here's my js:

function create() {

var htmlElements = "";
for (var i = 0; i < 5; i++) {
   htmlElements += '<div class="box">' + i + '</div>';
}
var container = document.getElementById("box");
container.innerHTML = htmlElements;

}

and html:

<body>

<div id="box">
<script>create();</script>
</div>

<script src="main.js"></script>

</body>




Aucun commentaire:

Enregistrer un commentaire