I would appreciate any help, I think I'm going about writing this code completely wrong. I'd like to change my header colour on the click of the button so it goes BLUE >click> RED >click > GREEN. So far I've only been able to achieve BLUE >click> GREEN. It never goes through three colours. Is there a different way to write the if statements? or a different method I do not know about that would allow me to have somewhat conditional css style changes?
var mySwitch = document.getElementById('header').style.background;
if (mySwitch == "darkblue") {
document.getElementById("myBtn").addEventListener("click", function(){
document.getElementById("header").style.background = "red";
});
}
else if (mySwitch == "red") {
document.getElementById("myBtn").addEventListener("click", function(){
document.getElementById("header").style.background = "green";
});
}
else {
document.getElementById("myBtn").addEventListener("click", function(){
document.getElementById("header").style.background = "darkblue";
});
}
* {
margin: 0;
padding: 0;
list-style: none;
font-family: sans-serif;
}
#header {
background: darkblue;
color: white;
display: flex;
justify-content: space-around;
height: 12vh;
}
#header h1 {
text-align: left;
margin-top: 30px;
}
.main-nav ul {
padding: 0px;
font-size: 1.5rem;
display: flex;
margin-top: 40px;
}
.main-nav a {
color: white;
padding: 20px;
text-decoration: none;
}
.main-nav a:hover {
color: rgb(255, 148, 148);
}
.hero-section {
background-image: url("https://stackoverflow.blog/wp-content/uploads/2018/01/BrutalLifeCycleJavascript.png");
background-position: center;
background-size: cover;
background-repeat: no-repeat;
height: 80vh;
color: white;
text-align: center;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
button {
background: black;
height: 40px;
width: 80px;
color: white;
margin-top: 20px;
animation: jumpbutton 1s ease-out infinite;
outline: none;
border-radius: 15px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="style.css" type="text/css">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
</head>
<body>
<header id="header">
<nav class="main-nav">
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</header>
<div class="hero-section">
<h1>JAVASCRIPT</h1>
<p id="paragraph">This is a testing environment</p>
<button type="submit" id="myBtn"> CLICK ME </button>
</div>
<div class="about-box">
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Quam, asperiores!</p>
</div>
<!-- CHANGE HEADER COLOR, BACKGROUND & HERO IMAGE-->
<!--- -->
<script src="main.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>