▶ TrainingGet Pro
All tools & lessonsAll toolsAllGet the appApp
Learning departments Music, Maths, Cookery, Programming and Art are open – explore →

JavaScript lesson 22: The web page: finding and changing elements

UK version. UK school years and UK spelling. · US 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 (Year 9, KS3), from Unit 9: The web page. It runs right here in your browser – nothing to install.

Time
40 minutes + 20 min homework
Year
Year 9, KS3 (ages 13–14)
You need
A computer or tablet, paper and a pencil
Marked
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 – marked straight away.

  1. Read what your child will learn together.
  2. 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.
  3. 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.
  4. 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

We are learning to 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, .class and 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

40 minutes in 5 parts
PartTimeHow it is checked
Starter5 minMarked by Classfern
Teaching input8 minRead, watch and talk
See it, then predict8 minMarked by Classfern
Predict, trace and check14 minMarked by Classfern
Plenary and exit ticket5 minMarked 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 marked instantly.

Practice tests

All JavaScript practice tests · All 24 JavaScript lessons

Curriculum

National curriculum in England, computing, key stage 3: pupils should be taught to use 2 or more programming languages, at least one of which is textual, to solve a variety of computational problems; make appropriate use of data structures [for example, lists, tables or arrays]; design and develop modular programs that use procedures or functions.

Contains public sector information licensed under the Open Government Licence v3.0.

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.