Event Handlers
Putting your CSS and JS in separate files.
Starting with this example:
<div id="container"></div>
<script>
const container = document.getElementById("container");
const kittenImg = document.createElement("img");
kittenImg.src = "http://placekitten.com/g/200/300";
container.appendChild(kittenImg);
</script>Move the JS to a separate file called script.js:
<script src="script.js"></script>What happens if...?
Put the
<script>tag before the<div>Put it after the
divPut it in the
<head>Keep it in the
<head>and adddeferto the tag - this is what we want to do going forward.
Show that we can also add stylesheets by linking them with the <link> tag (demo with a background color or something).
<head>
<link rel="stylesheet" href="styles.css" />
</head>Textboxes!
Extend the kitten example to use a textbox to do input.
Introducing input boxes with
<input type="text">Using
.valueto get the value of the text box in JavaScript
index.html
script.js
References
Last updated
Was this helpful?