This function is working. Every time I press the button it changes from "click me" to "click me again", but I want the button to be blue when it states "click me" and red when it's "click me again".
<!DOCTYPE html>
<html>
<body>
<button id="toggle" onclick="myFunction()" onclick="setcolor(blue)">click me</button>
<script>
function myFunction() {
var change = document.getElementById("toggle");
if (change.innerHTML == "click me again"){
change.innerHTML = "click me";
}
else {
change.innerHTML = "click me again";
}
}
</script>
</body>
</html>
Remove the second onclick listener from the button, then add the coloration code in your myFunction
function:
if (change.innerHTML == "click me again") {
change.innerHTML = "click me";
change.style.background='blue'
}
else {
change.innerHTML = "click me again";
change.style.background='red'
}
Set backgroundColor
on click just add one click function
<button id="toggle" onclick="myFunction()" >click me</button> <script> function myFunction() { var change = document.getElementById("toggle"); if (change.innerHTML == "click me again") { change.innerHTML = "click me"; change.style.backgroundColor = "blue" } else { change.innerHTML = "click me again"; change.style.backgroundColor = "red" } } </script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.