Change the Page's Text

Priya’s page has a place for everything the script will fill: an empty date-and-host line and summary line in the header, and empty lists for the players and the games, each with its own id. So far, though, main.js only writes to the Console, and visitors never open the Console. Priya wants the page itself to say when game night is and who’s hosting.

In this lesson, you’ll have main.js find an element by its id and change the text the page shows. You’ll be able to predict what a script that selects and changes elements does to the page and the Console, including when it finds nothing. Start in the Console, where you can try each step and see the page change at once.

Find Elements From the Console

Before a script can change part of the page, it has to get hold of that part. Here’s the page you finished in the exercise, with an empty games list. If your copy still holds Catan and Codenames, or the snacks list, that’s fine, as nothing in this lesson touches them:

Language: HTML Filename: scratch/index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Game Night</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <header>
    <h1>Priya's Game Night</h1>
    <p id="date-line"></p>
    <p id="summary"></p>
  </header>
  <main>
    <h2>Who's Coming</h2>
    <ul id="players"></ul>
    <h2>This Month's Games</h2>
    <ul id="games"></ul>
  </main>
  <script type="module" src="main.js"></script>
</body>
</html>

You’re about to show results on the page instead of in the Console, so open scratch/main.js and delete its two console.log() lines. Save the empty file, then open Priya’s page at http://127.0.0.1:3000/scratch/index.html and open its Console with Ctrl+Shift+J, or Cmd+Option+J on macOS.

In a page’s Console, the name document stands for the page the browser built, the tree you saw in the Elements panel. Its querySelector() method takes a CSS selector, the same kind you wrote in style.css, and gives back the first element on the page that matches it. Type this line into the Console and press Enter:

Language: JavaScript
document.querySelector("#date-line")

The Console echoes the empty paragraph, <p id="date-line"></p>. Hover over that echo and the date line lights up on the page. You’re holding the element itself, so you can read its text and change it.

Watch out for the #: it means “the element with this id”. Without it, querySelector("date-line") looks for a tag called date-line and finds none. Whenever querySelector() finds no match, it gives you null, JavaScript’s value for “no object here”.

Every element has a textContent property, which holds the text inside it. Read the heading’s text first, then set the summary line’s:

Language: JavaScript
document.querySelector("h1").textContent

The Console shows the heading’s text as a string:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.