How to change color: use buttons
P粉111927962
2023-09-05 16:38:19
<p>Is there a way for me to write fewer lines of code in this function? I want it to switch between the two colors when you press one of the buttons. The function green() is the exact opposite of red. </p>
<pre class="lang-html prettyprint-override"><code><h1 id="header">red or green</h1>
<button id="redButton" onclick="red()">red</button>
<button id="greenButton" onclick="green()">green</button>
</code></pre>
<pre class="brush:php;toolbar:false;">function red() {
document.getElementById("header").innerHTML = "red"
document.getElementById('header').style.color = "red"
document.getElementById('redButton').style.color = "white"
document.getElementById('redButton').style.backgroundColor = "red"
document.getElementById('greenButton').style.color = "black"
document.getElementById('greenButton').style.backgroundColor = "grey"
}</pre></p>