Section 1
by Louis Nguyen· Module 3

Root Folder
Not Attempted
NameProgress
JavaScript for Beginners Introduction
Not Read
Setting up environment
Not Read
Module 1: Engaging with JavaScript Fundamentals
Not Attempted
NameProgress
Section 1
Not Read
Section 2
Not Read
Section 3
Not Read
Module 1 Quiz
Not Attempted
Module 2: Introduction to Variables and Data Types
Not Attempted
NameProgress
Section 1
Not Read
Arithmetic Operations
Not Read
Type Conversion
Not Read
Section 1
Not Attempted
Section 2
Not Attempted
0 / 400 XP

In this lesson, you'll learn how to interact with the Document using JavaScript. We'll focus on selecting elements with document.getElementById and modifying their content with .textContent.

Selecting Elements with document.getElementById

The document.getElementById method is one of the simplest ways to select an HTML element. This method returns the element with the specified id attribute.

Syntax:

let element = document.getElementById(id);
  • id: The id attribute of the element you want to select.
  • element: The variable that stores the selected element.

Modifying Content with .textContent

Once you have selected an element, you can manipulate its content using the .textContent property. This property sets or returns the text content of the specified node and all its descendants.

Syntax:

element.textContent = newText; 
  • newText: The new text content you want to set for the element.
  • element: The variable that references the selected element.

Here’s a complete example demonstrating how to use document.getElementById to select an element and .textContent to modify its content:

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Loading...
Ready ...
Course Outline
Module 1: Engaging with JavaScript Fundamentals
Module 2: Introduction to Variables and Data Types
Module 3
Module 4
Module 5