Week 1
Learning goals
See this module's learning goals.
Relevant links
HTML interaction
Interacting with the HTML DOM is done through the document object in the browser. With the document object we can get html elements and change them.
For the next js part we use this html.
<html>
<body>
<h1>Javascript week 3</h1>
<div class="queue"></div>
<div id="test-id"></div>
<script src="js/review.js"></script>
</body>
</html>Code Commenting
First the straightforward part: how do we place comments in our code?
HTML
W3Schools Comments
CSS
JavaScript
Single line comments
Single line comments at end of the line:
Writing js documentation: JSDoc
When to comment?
Now for the hard part: when to comment? When you work for different companies, you will see different styles. Embrace something you like, and then learn to let go. Google on "when to comment code?" and you'll find a big bunch of different opinions.
The general concept is, however, that it is there to help make the code more easy to understand. Note, however, that comments can also make code more difficult to understand when not applied properly.
Events
Events in JavaScript are things like: A timer has just finished, a user clicked a button, our page has loaded, someone types into an input element or we have just gotten some data from a server. When these events happen, we usually want to add some functionality. For example, when a user clicks the "like" button (event), we want to increment the "like" counter, and color the "like" button blue. Or when someone clicks "Close cookies" (event) we want to remove the cookie div.
Let's first try to create some js that waits for 2 seconds and the console.logs out "2 seconds has elapsed!"
In JavaScript we use an event listener to listen:
Now let's keep track of how many times a button is clicked. To do something whenever a button gets clicked, we use what is called an event listener.
Imagine that the button is noisy. There's someone listening out for the click sound, and every time they hear it, they add 1 to a counter.
You might prefer to assign the event listener to a const, like this:
Last updated