JavaScript lesson 22: The web page: finding and changing elements
US version. US grades and US spelling. · UK version of this lesson
A web page is made of elements, like <p id="msg">Hello</p>. JavaScript sees the page as the DOM – the Document Object Model – and can find and change any element. A complete 40-minute JavaScript lesson for beginners (Grade 8), from Unit 9: The web page. It runs right here in your browser – nothing to install.
- Time
- 40 minutes + 20 min homework
- Grade
- Grade 8 (ages 13–14)
- You need
- A computer or tablet, paper and a pencil
- Graded
- Instantly, with explanations
For parents: how to use this lesson
You don't need to know JavaScript yourself. The lesson explains each step on screen, the way Mr Babbage, our Head of Computing, likes it: a worked example first, then a go for your child. Each question shows a small web page and some code, and your child says what it does – graded straight away.
- Read what your child will learn together.
- Press Start the lesson below. Sit with your child for the teaching and the worked example (about 16 minutes): ask them to say what each line does.
- Let them do the practice on their own. When code goes wrong, the error message is a clue – let them read it out and try again before you help.
- Finish with the talk question: Pick a web site you use. Which part of it do you think JavaScript changes?
Watch out for
querySelector("msg")finds id="msg". Without # it looks for a <msg> tag and gives null.textContent =adds to the text. = replaces; += adds.
Miss Lovelace's tip for home: ask your child to explain one line of their program to you in their own words. If they can explain it, they understand it – and every error message is a clue, not a failure.
What your child will learn
Learning goal: find an element on a web page with querySelector.
Success looks like
- I can find an element on a web page with
querySelector. - I can read and change its text with
textContent. - I can explain
#id,.classand tag selectors.
Key words
DOM · element · selector · querySelector · textContent · null
What to have ready
- A device with a web browser (the code runs in the browser – nothing to install)
- Paper for tracing
- A keyboard helps for typing code (a tablet or phone works too)
The big ideas (for the adult)
A web page is made of elements, like <p id="msg">Hello</p>. JavaScript sees the page as the DOM – the Document Object Model – and can find and change any element.
document.querySelector("#msg") finds the first element with id="msg". ".note" finds by class, and a plain word like "li" finds by tag. querySelectorAll finds them all. If nothing matches, you get null.
el.textContent is the element's text. el.textContent = "Bye" REPLACES it; += adds to it.
Worked example – see it
// <h1 id="title">Our trip</h1>
// <p id="msg" class="note">Hello Ben</p>
// <ul id="list"><li>rabbit</li><li class="star">cat</li><li>frog</li></ul>
// <button id="go">Click me</button>
const items = document.querySelectorAll("li")
console.log(items.length)It prints:
3
The lesson, step by step
| Part | Time | How it is checked |
|---|---|---|
| Starter | 5 min | Graded by Classfern |
| Teaching input | 8 min | Read, watch and talk |
| See it, then predict | 8 min | Graded by Classfern |
| Predict, trace and check | 14 min | Graded by Classfern |
| Closure and exit ticket | 5 min | Graded by Classfern + self-check |
The lesson
It runs here on the page. No sign-up and no names – progress is kept only in this browser. Each visit picks a new mix of questions.
Homework
20 minutes: 7 questions – predict what page code does, spot what goes wrong, and check the key ideas – then a short reflection. New questions every time, all graded instantly.
Practice tests
Curriculum
CSTA K–12 Computer Science Standards (2017), grades 6–8: 2-AP-16 – Use existing code, media and libraries in original programs, with attribution.
The standard codes are CSTA's; the descriptions are our own words.
Our lessons and questions are our own. They are not written or endorsed by any government department, testing agency or exam board. JavaScript is a trademark or registered trademark of Oracle Corporation in the U.S. and other countries; Classfern is not affiliated with or endorsed by it.