Last updated: Mar 5, 2024
Reading timeยท4 min
To change the page's background color on click:
click
event listener to an element.document.body.style.backgroundColor
property to a specific color.Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div id="box">Some text here</div> <button id="btn">Button</button> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { // ๐๏ธ change background color document.body.style.backgroundColor = 'salmon'; // ๐๏ธ optionally change text color // document.body.style.color = 'white'; });
click
event listener to the button, so a function is invoked every time the button is clicked.Each time the button is clicked, we set the
document.body.style.backgroundColor
property to salmon
and change the page's
background color.
To change an element's background color on click:
click
event listener to the element.event
object to a variable in the function.event.target.style.backgroundColor
property to the specific
background color.const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { // ๐๏ธ change background color event.target.style.backgroundColor = 'salmon'; // ๐๏ธ optionally change text color // event.target.style.color = 'white'; });
Every time the button from the example is clicked, its own background color gets set.
We used the
target property
on the event
object. The target
property is a reference to the object
(element) on which the event was dispatched.
event.target
gives us access to the DOM element the user clicked.You can console.log
the target
property to see the DOM element which was
clicked by the user.
const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { console.log(event.target); // ๐๏ธ change background color event.target.style.backgroundColor = 'salmon'; // ๐๏ธ optionally change text color // event.target.style.color = 'white'; });
If you click on the button and look at your console
output, you'll see the
button
element being logged.
To change another element's background color on click:
click
event listener to one of the elements.style.backgroundColor
property
of the other element.Here is the HTML for the example.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <div id="box">Some text here</div> <button id="btn">Button</button> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { const box = document.getElementById('box'); box.style.backgroundColor = 'coral'; // ๐๏ธ optionally change text color // box.style.color = 'white'; });
Each time the button is clicked, we change the div
's background color to
coral
.
To toggle an element's background color on click:
click
event listener to the element.element.style.backgroundColor
property to change the element's
background color.Here is the HTML for the examples.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>bobbyhadz.com</title> </head> <body> <button id="btn" style="background-color: salmon">Button</button> <script src="index.js"></script> </body> </html>
And here is the related JavaScript code.
const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { const backgroundColor = btn.style.backgroundColor; if (backgroundColor === 'salmon') { btn.style.backgroundColor = 'green'; // ๐๏ธ optionally change text color // btn.style.color = 'white'; } else { btn.style.backgroundColor = 'salmon'; // ๐๏ธ optionally change text color // btn.style.color = 'blue'; } });
We added a click
event listener to the button element, so a function is
invoked every time the button is clicked.
If the element's background color is not equal to the value, we reset the background color to its initial value.
You could add more conditions by using an else if
statement.
const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { const backgroundColor = btn.style.backgroundColor; if (backgroundColor === 'salmon') { btn.style.backgroundColor = 'green'; } else if (backgroundColor === 'green') { btn.style.backgroundColor = 'purple'; } else { btn.style.backgroundColor = 'salmon'; } });
In the example above the background colors of the element alternate between
salmon
, green
and purple
.
Note that instead of explicitly using btn
, we can use the target
property on
the event
object to access the element the user clicked on.
const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { const backgroundColor = event.target.style.backgroundColor; if (backgroundColor === 'salmon') { event.target.style.backgroundColor = 'green'; } else { event.target.style.backgroundColor = 'salmon'; } });
In the example, we used the
target property
on the event
object. The target
property is a reference to the object
(element) on which the event was dispatched.
event.target
gives us access to the DOM element the user clicked.Because the event listener is added to the button element, event.target
points
to the button.
You can console.log
the target
property to see the DOM element which was
clicked by the user.
const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { console.log(event.target); const backgroundColor = event.target.style.backgroundColor; if (backgroundColor === 'salmon') { event.target.style.backgroundColor = 'green'; } else { event.target.style.backgroundColor = 'salmon'; } });
Using event.target
is a bit more generic and is especially useful when adding
events to larger elements, e.g. the document
.
You can learn more about the related topics by checking out the following tutorials: