Week 2
Last week, we used HTML and CSS to build a personal page. HTML gave us the content, and CSS let us change how it looked. If we wanted to change the text on the page, we had to edit the HTML file and refresh the browser.
JavaScript is a programming language that lets us change a page while someone is using it. For example, we can respond to a button click by showing a message. We will add that to the same page we made last week.
Here is what our page will look like before and after clicking the button:
Getting started
Open last week's folder in your editor and open in your browser. Keep the editor and browser next to each other so you can see what happens as you work. If you missed last week or cannot find your files, ask for a copy of the Week 1 example.<span class="editor-theme-code">index.html</span>
Adding the button
First, we need a button to click and a paragraph where we can show the fact. Add these inside <, just before span class="editor-theme-code"><main></span><:span class="editor-theme-code"></main></span>
<p><button id="fact-button" type="button">Show a fun fact</button></p>
<p id="fact" aria-live="polite">Your fact will appear here.</p>
The first line adds a button inside a paragraph, which keeps it on its own line. makes it a regular button. Because we used the HTML <span class="editor-theme-code">type="button"</span>< element, the browser already lets someone reach it with the keyboard and activate it with Enter or Space.span class="editor-theme-code"><button></span>
The attributes give these elements unique names on the page. Our button's ID is <span class="editor-theme-code">id</span>, and our message's ID is <span class="editor-theme-code">fact-button</span>. JavaScript will use those IDs to find the right elements, so make sure each spelling matches the example.<span class="editor-theme-code">fact</span>
A screen reader reads page content aloud. tells it to announce changes to the paragraph when it has finished what it is already reading. This is helpful because someone using a screen reader might otherwise miss the new message.<span class="editor-theme-code">aria-live="polite"</span>
Save the file and refresh the page. You should see both elements, but clicking the button will not change anything yet. We still need to write the JavaScript that handles the click.
Adding JavaScript to the page
Create a file called in the same folder as <span class="editor-theme-code">script.js</span> and <span class="editor-theme-code">index.html</span>. The <span class="editor-theme-code">styles.css</span> ending tells us this is a JavaScript file.<span class="editor-theme-code">.js</span>
In , add the following line just before <span class="editor-theme-code">index.html</span><:span class="editor-theme-code"></body></span>
<script src="script.js"></script>
This tells the browser where to find our JavaScript. We put it at the end of the body so the browser reads the button and paragraph before running code that tries to find them. Save both files.
Finding elements
To change one paragraph, JavaScript needs a way to find it on the page. The browser organizes the HTML elements into the Document Object Model, usually shortened to DOM. We can use the DOM to find our paragraph and change its text.
A program works with values, such as text, numbers, or elements from a page. A variable gives a value a name so we can use it later in our code. In this example, we want names for our button and paragraph.
Add these lines to :<span class="editor-theme-code">script.js</span>
const factButton = document.querySelector("#fact-button");
const fact = document.querySelector("#fact");
refers to the current page. <span class="editor-theme-code">document</span> finds the first element that matches a selector. The selector tells it which element to look for. Here, <span class="editor-theme-code">querySelector</span> means an ID, so <span class="editor-theme-code">#</span> finds the element with <span class="editor-theme-code">"#fact-button"</span>.<span class="editor-theme-code">id="fact-button"</span>
creates a variable and prevents us from giving it a different value later. The <span class="editor-theme-code">const</span> gives the name on the left the value on the right. Here, <span class="editor-theme-code">=</span> refers to the button that <span class="editor-theme-code">factButton</span> found. The semicolon ends the instruction.<span class="editor-theme-code">querySelector</span>
The second line does the same thing for our paragraph, using the name . We can now use <span class="editor-theme-code">fact</span> and <span class="editor-theme-code">factButton</span> throughout the file without looking up the elements again.<span class="editor-theme-code">fact</span>
Compare each selector with its HTML ID. Notice that the appears in the JavaScript selector, but not in the HTML ID itself. Also, <span class="editor-theme-code">#</span> is a name we chose for our variable. It does not have to be spelled the same way as the ID.<span class="editor-theme-code">factButton</span>
Functions
Now that we have the paragraph, we need to describe what should happen to it. A function groups instructions under a name. Calling the function means telling it to run those instructions.
We will make a function called . Add this below the two variables:<span class="editor-theme-code">showFact</span>
function showFact() {
fact.textContent = "I can solve a Rubik's Cube.";
}
tells JavaScript we are defining a function, and <span class="editor-theme-code">function</span> is its name. The instructions go inside the braces, <span class="editor-theme-code">showFact</span> and <span class="editor-theme-code">{</span>. The parentheses are empty because this function does not need us to give it any inputs.<span class="editor-theme-code">}</span>
Inside the function, refers to the text in our paragraph. The <span class="editor-theme-code">fact.textContent</span> replaces that text with the value on the right. Text inside quotation marks is called a string. Here, our string is <span class="editor-theme-code">=</span>. The browser displays the sentence without the surrounding quotation marks.<span class="editor-theme-code">"I can solve a Rubik's Cube."</span>
Earlier, we used for <span class="editor-theme-code">const</span>. We can still change the paragraph's text because <span class="editor-theme-code">fact</span> continues to refer to the same element. We are changing that element's content, rather than giving the variable a different element.<span class="editor-theme-code">fact</span>
At this point, we have described what does, but we have not called it. If you refresh the page, the starting message will still be there.<span class="editor-theme-code">showFact</span>
Events
An event is something that happens on a page, such as someone clicking a button. An event listener lets us run a function when a particular event happens.
We want to run when someone clicks our button. Add this line below the function's closing brace:<span class="editor-theme-code">showFact</span>
factButton.addEventListener("click", showFact);
In this example, we add the listener to . <span class="editor-theme-code">factButton</span> is the event we want to respond to, and <span class="editor-theme-code">"click"</span> is the function we want to run.<span class="editor-theme-code">showFact</span>
Notice that we wrote without parentheses here. That gives the listener a function it can call later. Writing <span class="editor-theme-code">showFact</span> would call the function immediately while the page loads, instead of giving the listener the function to use when someone clicks.<span class="editor-theme-code">showFact()</span>
Save both files and refresh the page. Click the button. You should now see the fact appear. Here is the complete so you can compare it with your file:<span class="editor-theme-code">script.js</span>
const factButton = document.querySelector("#fact-button");
const fact = document.querySelector("#fact");
function showFact() {
fact.textContent = "I can solve a Rubik's Cube.";
}
factButton.addEventListener("click", showFact);
Try clicking the button again, then refresh the page. Each click sets the same message. Refreshing brings back the starting text because our JavaScript changes the page in the browser, not the saved HTML file.
Changing the message
Replace the fact with something about yourself. Change the words inside the double quotation marks in , and keep the surrounding punctuation in place. You can also change the button's visible text in <span class="editor-theme-code">showFact</span>. Keep the button's ID the same so the selector still finds it.<span class="editor-theme-code">index.html</span>
Save, refresh, and try the button. Then show the person next to you which line changes the message and which line connects it to the click.
Try using the keyboard too. Press Tab to move between links and buttons. When you reach the button, press Enter or Space. If your browser skips it, ask for help with its keyboard navigation setting.
The screenshots also use button styles. If you want the same appearance, copy the two rules beginning with to the end of your <span class="editor-theme-code">button</span>. They change how the button looks, and our JavaScript works with or without them.<span class="editor-theme-code">styles.css</span>
Looking things up
Let's say you knew you wanted to change a paragraph's text, but did not know about textContent. You could search Google for JavaScript textContent W3Schools.
Try that search and open the W3Schools result about textContent. Its example shows how to change an element's text. If you cannot find the result, use the W3Schools textContent reference. Use MDN's textContent reference for more detail about how it works.
Find the example that gives an element new text. Use it to replace the line inside your existing showFact function. Our paragraph is already stored in fact, so use that name and choose a different message.
Save and try your page again. Compare your line with the documentation example. The element name and message may be different, but both lines use textContent to change text. This is how we can take an example from documentation and use it in our own project.
Debugging
Debugging means finding and fixing a problem in our code. Let's make a small mistake so we can see what the browser tells us about it.
In the first line of , remove one <span class="editor-theme-code">script.js</span> from <span class="editor-theme-code">t</span>:<span class="editor-theme-code">fact-button</span>
const factButton = document.querySelector("#fact-buton");
Save, refresh, and try clicking. The button is still there, but it no longer changes the message.
Right-click the page, choose Inspect, and open the Console tab. The console shows errors from JavaScript. Look for the error connected to . Its wording may vary by browser, but you may see <span class="editor-theme-code">script.js</span> and <span class="editor-theme-code">null</span>.<span class="editor-theme-code">addEventListener</span>
returns <span class="editor-theme-code">querySelector</span> when it cannot find a matching element. Our HTML still has <span class="editor-theme-code">null</span>, but our JavaScript now looks for <span class="editor-theme-code">id="fact-button"</span>. As a result, <span class="editor-theme-code">fact-buton</span> contains <span class="editor-theme-code">factButton</span>, and the next part of our code cannot attach a listener to it.<span class="editor-theme-code">null</span>
The error points to the line where JavaScript tried to add the listener. The actual mistake is earlier, in the selector. Fix the missing , save, and refresh. The button should work again.<span class="editor-theme-code">t</span>
If your code stops working, start with what you expected to happen and what happened instead. The console often gives you a place to look. Change one thing at a time so you can tell which change fixed the problem.
Before next week
Show someone your finished interaction and talk through how the button, listener, and function connect. Keep these files. Next week, we will use Git and GitHub to save changes and review each other's work.
Before that meeting, follow the GitHubWeek 3 setup checklist to prepareinstall yourGit account.and FIXGitHub THECLI, URLsign ANDin REWRITEfrom SETUPthe VS Code terminal, and find a partner. Keep the page in its current folder. We will turn that folder into a repository and publish it together. If you cannot finish setup, come anyway and tell me at the start.
Resources
Start with W3Schools for a short example. Use MDN for more detail.
What is JavaScript? —W3Schools: JavaScript Introduction
<spanMDN: class="editor-theme-code">querySelector()</span><spanMDN: class="editor-theme-code">const</span><spanMDN: class="editor-theme-code">textContent</span><span class="editor-theme-code">addEventListener()</span><span class="editor-theme-code">aria-live</span>

