Day 4
Sign back in!
Username: first_lastInitial (example: rohan_b)
Password: ilovecoding!
Learn how JavaScript can change a website!
Then use what you learned to invent a clicker.
JS stands for JavaScript.
It lets us make our websites interactive!
index.html - the content
style.css - the appearance
script.js - the actions
Make a new file called:
script.jsKeep it next to your HTML file.
In index.html, add this right before </body>:
<script src="script.js"></script>
In script.js, type:
alert('JavaScript is working!');
Save, then refresh your website.
You should see a pop-up message.
If not, check your file name and your script link.
Remove the test alert when you're done.
A variable stores information.
Think of it as a labeled box!
let apples = 2;
We made a variable named apples.
It starts at 2.
apples = apples + 1;
Take the old number and add 1.
let apples = 2;
apples = apples + 1;
apples = apples + 1;
alert(apples);
What will the alert say?
let name = 'Sam';
let score = 10;
Text uses quotes. Numbers do not!
A function is a group of instructions.
We can run those instructions whenever we want!
function sayHello() {
alert('Hello!');
}
sayHello();
Writing the function creates it.
Calling it runs it!
In your HTML file:
<button onclick="sayHello()">Say Hello</button>
Clicking the button calls sayHello().
index.html: the button
script.js: the function
Our script link connects them!
JavaScript can do more than show alerts!
Remember, HTML makes the parts of a webpage.
<h1>My Website</h1>
<p>Welcome!</p>
We can use:
document.querySelector('h1');
This finds the first <h1> on the page.
document = our webpage
querySelector = find an element
'h1' = what we want to find
Give an HTML element a unique name with id.
<p id="message">Hello!</p>
Now we can find this exact paragraph.
document.querySelector('#message');
The # means we are looking for an ID.
document.querySelector('p');
document.querySelector('#message');
document.querySelector('.special');
p = tag, # = ID, . = class
querySelector finds the first match.
let message = document.querySelector('#message');
Now message refers to our HTML paragraph!
Let's make JavaScript change what we see!
textContent is the text inside an element.
<p id="message">Hello!</p>
Right now, its text is Hello!
document.querySelector('#message').textContent = 'Goodbye!';
The paragraph now says Goodbye!
let message = document.querySelector('#message');
message.textContent = 'Welcome back!';
Both ways do the same thing.
let temperature = 72;
document.querySelector('#weather').textContent = temperature;
We can display information stored in variables.
let fruit = 'apple';
fruit = 'banana';
document.querySelector('#food').textContent = fruit;
What text will appear in the element with id="food"?
Make a heading with an ID in your HTML.
Use querySelector to change its text from JavaScript.
Can you change it again?
Remember: buttons can run functions.
In index.html:
<button onclick="sayHello()">Say Hello</button>
In script.js:
function sayHello() {
alert('Hello!');
}
function showSurprise() {
document.querySelector('#surprise').textContent = 'Surprise!';
}
What could call this function?
let coins = 3;
function findCoin() {
coins = coins + 1;
}
What happens to coins when we call findCoin()?
Changing a JavaScript variable does not automatically change the webpage.
Use textContent when you want to change visible text.
Check your file name and script link.
Check your spelling and selectors.
Make sure the HTML element exists!
Save and refresh.
Use today's ideas to build your own clicker!
Press a button to earn points.
Show the current points on your webpage.
Make it your own!
How can JavaScript remember a number?
How can a button run your code?
How can JavaScript find and change text?
script.js.