nuisance nuisance - 3 months ago 6
Javascript Question

How to change the text color with button click?

I have a text like following :

<p><strong><em> QUESTION: WHAT IS YOUR NAME?</em></strong></p>
<button>COLORCHANGER<button>


I want to change the text color when I click the button.

Answer

Attach click event to the button using addEventListener('click') then change the color of your text using .style.color = 'color', check example below.

NOTE : It will be better if you give your elements an identifier.

Hope this helps.

document.querySelector('button').addEventListener('click', function(){
    document.querySelector('p').style.color='green';
})
<p><strong><em> QUESTION: WHAT IS YOUR NAME?</em></strong></p>
<button>COLORCHANGER</button>

Comments