Getting started with JavaScript
-
Create a new folder on your desktop. Name it something like
learning_js.
-
Open the folder in VS Code. Add a new
index.html file.
Copy this example code into the index.html file.
-
Create another new file in the
learning_js folder and name it script.js.
-
Add the following line of code at the end of the
body of your .html file, just before the closing </body> tag:
<script src="script.js"></script>
-
Add the following line of code in the
script.js file:
console.log("hello world!");
-
Save both files, then open the html file in Firefox or Google Chrome.
-
Inspect the page (right click anywhere on the webpage and click
Inspect, or press Ctrl+Shift+I / Command+Option+I) to open the developer tools.
-
Click on the Console tab:
-
You should see your JavaScript output logged in the console!
Exercise: log stuff in the console
- log your name
-
log the numbers 1 through 20 without typing each number individually
-
log “beep” if the current time is more than 30 minutes after the hour and log “boop” if the current time is less than or equal to 30 minutes after the hour
Once you have attempted the exercises on your own, see sample solutions here.
Basics of JavaScript
JavaScript is a programming language designed for building functional, interactive web pages. It is a widely popular programming language that can do many different things.
Document Object Model (DOM)
The "DOM" is the structure that allows JavaScript code to interact with HTML elements on a webpage. In this model, every element in an HTML document is an "object" that has certain properties and can be acted on via certain methods.
You can use JavaScript code to edit or add functionality to elements on your HTML page using object.method() and object.property syntax. It's probably best to just start by looking at some examples:
-
Selecting HTML elements:
document.getElementById(“myID”);
document.getElementsByClassName(“myClass”);
Check for understanding: what is different about the names of these two methods?
-
Reading HTML text:
let greeting = document.getElementById(“greeting”);
console.log(greeting);
Check for understanding: when/why do we write "greeting" in quotation marks vs just greeting as just a word?
-
Changing HTML elements:
greeting.innerText = “howdy universe”;
let greenElements = document.getElementsByClassName(“greenText”);
for (let i = 0; i < greenElements.length; i++) {
greenElements[i].style.color = “green”;
}
Check for understanding: why do we need a for() loop to change greenElements but not greeting?
-
Write a function that makes desired changes when an HTML element is clicked:
function turnRed() {
let listItems = document.getElementsByTagName("li");
for (let i = 0; i < listItems.length; i++) {
listItems[i].style.color = "red";
}
}
Call your custom turnRed() function in your .html file:
<button onclick="turnRed()">click me!</button>
Check for understanding: what language is this^?
Exercise: add a button to your webpage that changes a property of HTML element(s). For example, you could try to create:
- a button that replaces some text with a different string of text
- two buttons: one that turns some text red and another that turns some text green
- a button that toggles some piece of text between red and green (or any two colors)
- a button that does anything you want it to do! the internet is your oyster ☺︎