Change Text Color On Click Using JavaScript | Bobbyhadz
Có thể bạn quan tâm
# Table of Contents
- Change the document's text color on click
- Change the element's text color on click
- Change another element's text color on click
# Change the document's text color on click
To change the document's text color on click:
- Add a click event listener to an element.
- Each time the element is clicked, set the document.body.style.color property to a specific color.
- The text color change will be global unless overridden.
Here is the HTML for the example.
index.htmlCopied!<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <div>Some text here</div> <button id="btn">Button</button> <script src="index.js"></script> </body> </html> The code for this article is available on GitHubAnd here is the related JavaScript code.
index.jsCopied!const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { // 👇️ Change text color globally document.body.style.color = 'darkgreen'; // 👇️ Change text color for clicked element only // event.target.style.color = 'salmon'; }); The code for this article is available on GitHubWe added a 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.color property to darkgreen and change the text color globally.
# Change the element's text color on click
To change an element's text color on click:
- Add a click event listener to the element.
- Assign the event object to a variable in the function.
- Set the event.target.style.color property to the specific text color.
Every time the button is clicked, its own text 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.
In other words 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.
index.jsCopied!const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { console.log(event.target); // 👇️ Change text color for clicked element only event.target.style.color = 'salmon'; });If you click on the button and look at your console output, you'll see the button element being logged.
# Change another element's text color on click
To change another element's text color on click:
- Add a click event listener to one of the elements.
- Each time the element is clicked, change the style.color property of the other element.
Let's add an id to the div element, so we can select it.
index.htmlCopied!<!DOCTYPE html> <html lang="en"> <head> <title>bobbyhadz.com</title> <meta charset="UTF-8" /> </head> <body> <div id="box">Some text here</div> <button id="btn">Button</button> <script src="index.js"></script> </body> </html> The code for this article is available on GitHubAnd here is the related JavaScript code.
index.jsCopied!const btn = document.getElementById('btn'); btn.addEventListener('click', function onClick(event) { const box = document.getElementById('box'); box.style.color = 'salmon'; }); The code for this article is available on GitHubEach time the button is clicked, we change the div's text color to salmon.
I've also written an article on how to change text color on mouseover.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
- How to Change Text color on Mouseover in JavaScript
- Add a class to the Clicked Element using JavaScript
- Add a class to multiple Elements using JavaScript
- Add class to a parent Element using JavaScript
- Add event listener to all Elements with Class in JavaScript
- Change a Style of all Elements with a specific Class in JS
Từ khóa » Html Color Text Javascript
-
JavaScript String Fontcolor() Method - W3Schools
-
HTML DOM Style Color Property - W3Schools
-
Change Text Color With Javascript? - Stack Overflow
-
How To Change The Font Color Of A Text Using JavaScript?
-
How To Change Font Color Of HTML Element In JavaScript?
-
How To Change The Text Color In JavaScript - Reactgo
-
Text Color Change In Js Code Example - Code Grepper
-
JavaScript: String Fontcolor() Method - TechOnTheNet
-
ntcolor() - JavaScript - MDN Web Docs
-
- HTML: HyperText Markup Language | MDN"> - HTML: HyperText Markup Language | MDN
-
How To Change Text Color In HTML? - Top 3 Ways To Create Awesome ...
-
How To Change Text Color In Html - Javatpoint
-
How To Change Text Color On Click Using JavaScript?
-
How To Change Text Color In Javascript - YouTube