Coder Dojo

Day 4


Instructors: Rohan, Pranay

Open up Coder

code.devoflife.com

Sign back in!

Username: first_lastInitial (example: rohan_b)

Password: ilovecoding!

Today's Goal

Learn how JavaScript can change a website!

Then use what you learned to invent a clicker.

JS Files

Let's give JavaScript its own file.

What is JS again?

JS stands for JavaScript.

It lets us make our websites interactive!

Our Files

index.html - the content

style.css - the appearance

script.js - the actions

Create a File

Make a new file called:

script.js

Keep it next to your HTML file.

Link It!

In index.html, add this right before </body>:


<script src="script.js"></script>
            

Test It

In script.js, type:


alert('JavaScript is working!');
            

Save, then refresh your website.

Did It Work?

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.

Variables

A variable stores information.

Think of it as a labeled box!

Make a Variable


let apples = 2;
            

We made a variable named apples.

It starts at 2.

Change a Variable


apples = apples + 1;
            

Take the old number and add 1.

Try to Predict!


let apples = 2;
apples = apples + 1;
apples = apples + 1;
alert(apples);
            

What will the alert say?

Text or Number?


let name = 'Sam';
let score = 10;
            

Text uses quotes. Numbers do not!

Functions

A function is a group of instructions.

We can run those instructions whenever we want!

Make a Function


function sayHello() {
  alert('Hello!');
}
            

Run a Function


sayHello();
            

Writing the function creates it.

Calling it runs it!

Run It with a Button

In your HTML file:


<button onclick="sayHello()">Say Hello</button>
            

Clicking the button calls sayHello().

Where Does the Code Go?

index.html: the button

script.js: the function

Our script link connects them!

Changing the Page

JavaScript can do more than show alerts!

HTML Elements

Remember, HTML makes the parts of a webpage.


<h1>My Website</h1>
<p>Welcome!</p>
            

How Does JS Find Them?

We can use:


document.querySelector('h1');
            

This finds the first <h1> on the page.

What Does It Mean?

document = our webpage

querySelector = find an element

'h1' = what we want to find

IDs

Give an HTML element a unique name with id.


<p id="message">Hello!</p>
            

Now we can find this exact paragraph.

Finding an ID


document.querySelector('#message');
            

The # means we are looking for an ID.

Other Selectors


document.querySelector('p');
document.querySelector('#message');
document.querySelector('.special');
            

p = tag, # = ID, . = class

querySelector finds the first match.

Save the Element


let message = document.querySelector('#message');
            

Now message refers to our HTML paragraph!

Changing Text

Let's make JavaScript change what we see!

textContent

textContent is the text inside an element.


<p id="message">Hello!</p>
            

Right now, its text is Hello!

Change the Text!


document.querySelector('#message').textContent = 'Goodbye!';
            

The paragraph now says Goodbye!

Or Use a Variable


let message = document.querySelector('#message');
message.textContent = 'Welcome back!';
            

Both ways do the same thing.

Numbers Work Too


let temperature = 72;
document.querySelector('#weather').textContent = temperature;
            

We can display information stored in variables.

Try to Predict!


let fruit = 'apple';
fruit = 'banana';
document.querySelector('#food').textContent = fruit;
            

What text will appear in the element with id="food"?

Mini Experiment

Make a heading with an ID in your HTML.

Use querySelector to change its text from JavaScript.

Can you change it again?

Make it Interactive

Remember: buttons can run functions.

Button + Function

In index.html:


<button onclick="sayHello()">Say Hello</button>
            

In script.js:


function sayHello() {
  alert('Hello!');
}
            

Functions Can Change HTML


function showSurprise() {
  document.querySelector('#surprise').textContent = 'Surprise!';
}
            

What could call this function?

Functions Can Change Variables


let coins = 3;

function findCoin() {
  coins = coins + 1;
}
            

What happens to coins when we call findCoin()?

Important Difference!

Changing a JavaScript variable does not automatically change the webpage.

Use textContent when you want to change visible text.

If Something Doesn't Work...

Check your file name and script link.

Check your spelling and selectors.

Make sure the HTML element exists!

Save and refresh.

Your Challenge

Use today's ideas to build your own clicker!

What Is a Clicker?

Press a button to earn points.

Show the current points on your webpage.

Make it your own!

Think About It...

How can JavaScript remember a number?

How can a button run your code?

How can JavaScript find and change text?

Your Task...

  1. Create and link script.js.
  2. Make a clicker that updates a number on your page.
  3. Figure out how to make a clicker shop (hint: variables)
  4. Style it using CSS!