Animations

API

Arrays

Async

Basics

Challenges

Classes

Console

Dates

Debugging

DOM Elements

DOM Methods

DOM Navigation

DOM Properties

Event Listeners

Flow Control

Forms

Functions

Global Functions

JSON

Keywords

Libraries (3rd party)

Math

Modules

Objects

Snippets

String

Types

Widgets

Window Object

JavaScript - Get Element By Id

The getElementById() is a popular DOM method to get an HTML element object. It is a method of the document object. You don't necessarily need to assign the results of the method to a variable (as I commonly do). You can simply find it and manipulate it.


Demo 1


let demo1 = document.getElementById("demo1");
demo1.style.color = "red";

//or

document.getElementById("demo2").style.color = "blue";
document.getElementById("demo2").innerHTML = "blue!";
document.getElementById("img1").src = "/assets/images/javascript-logo.png";