Name | Progress |
---|---|
JavaScript for Beginners Introduction | Not Read |
Setting up environment | Not Read |
Name | Progress |
---|---|
Section 1 | Not Read |
Section 2 | Not Read |
Section 3 | Not Read |
Module 1 Quiz | Not Attempted |
Name | Progress |
---|---|
Section 1 | Not Read |
Arithmetic Operations | Not Read |
Type Conversion | Not Read |
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.
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);
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;
Here’s a complete example demonstrating how to use document.getElementById to select an element and .textContent to modify its content: