Issue
I want it so that when the button is pressed, it resets the value of textPlacement.innerHTML
to an empty string, ensuring there is no text. The issue is that currently, when I press the button, it adds the new value to the right of the previous values. Please keep in mind that I want to retain the HTML text "Background Color: ".
let textPlacement = document.getElementById("insert-colour");
let simpBackgroundColour = (Math.floor(Math.random() * 3)) + 1;
textPlacement.innerHTML += "";
if (simpBackgroundColour === 1) {
document.body.style.background = "#eb1e17";
textPlacement.innerHTML += "Red";
} else if (simpBackgroundColour === 2) {
document.body.style.background = "#2390de";
textPlacement.innerHTML += "Blue";
} else {
document.body.style.background = "#d9d923";
textPlacement.innerHTML += "Yellow";
}
<h class="background-colour-box" id="insert-colour">Background Colour : </h>
<div class="button">Click Me</div>
Solution
Why not just set "Background Color: color" on each if?
let textPlacement = document.getElementById("insert-colour");
let simpBackgroundColour = (Math.floor(Math.random() * 3)) + 1;
if (simpBackgroundColour === 1) {
document.body.style.background = "#eb1e17";
textPlacement.innerHTML += "Background Colour :Red";
} else if (simpBackgroundColour === 2) {
document.body.style.background = "#2390de";
textPlacement.innerHTML += "Background Colour : Blue";
} else {
document.body.style.background = "#d9d923";
textPlacement.innerHTML += "Background Colour : Yellow";
}
<h class="background-colour-box" id="insert-colour">Background Colour : </h>
<div class="button">Click Me</div>
This will keep Background Color on whatever statement matches, and will rewrite the innterHTML each time.
Answered By - eskil4152
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.