Welcome to Coding Torque. In this blog, we are going to create a Background Color Switcher using HTML, CSS and JavaScript. You should create this project if you are a beginner and learning JavaScript.
Before we start, here are some more JavaScript projects for you to practice:
1. Filter Cards using JavaScript
2. Typing Effect using JavaScript
3. Email Validator using JavaScript
4. Tip Calculator using JavaScript
5. QR Code Generator using JavaScript
I would recommend you don't just copy and paste the code, just look at the code and type by understanding it.
Let's go step by step:
Step 1: HTML Code
Starter Template
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Google Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Poppins&display=swap" rel="stylesheet">
<!-- CSS -->
<link rel="stylesheet" href="style.css">
<title>OnClick Background Color Change with Source Code - @code.scientist x @codingtorque</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
Paste the below code in your <body> tag
<div class="container">
<h1>Background Color Switcher</h1>
<div>
<span class="button" id="grey"></span>
<span class="button" id="deepskyblue"></span>
<span class="button" id="blue"></span>
<span class="button" id="yellow"></span>
<span class="button" id="red"></span>
<span class="button" id="green"></span>
</div>
</div>
Output Till Now
Step 2: CSS Code
Create a file style.css and paste the code below.
html {
margin: 0;
font-family: "Poppins", sans-serif;
}
body {
display: flex;
align-items: center;
justify-content: center;
margin-top: 15rem;
}
.container {
background: white;
width: 50rem;
padding: 4rem 0;
border-radius: 10px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
span {
display: block;
cursor: pointer;
}
.button {
width: 100px;
height: 60px;
border: solid black 2px;
display: inline-block;
border-radius: 10px;
}
#grey {
background: grey;
}
#deepskyblue {
background: deepskyblue;
}
#blue {
background: blue;
}
#yellow {
background: yellow;
}
#red {
background: red;
}
#green {
background: green;
}
Output Till Now
Step 3: JavaScript Code
Create a file script.js and paste the code below.
const buttons = document.querySelectorAll('.button');
const body = document.querySelector('body');
console.log(buttons);
buttons.forEach(function (button) {
button.addEventListener('click', function (e) {
console.log(e.target);
if (e.target.id === 'grey') {
body.style.backgroundColor = e.target.id;
}
if (e.target.id === 'deepskyblue') {
body.style.backgroundColor = e.target.id;
}
if (e.target.id === 'blue') {
body.style.backgroundColor = e.target.id;
}
if (e.target.id === 'yellow') {
body.style.backgroundColor = e.target.id;
}
if (e.target.id === 'red') {
body.style.backgroundColor = e.target.id;
}
if (e.target.id === 'green') {
body.style.backgroundColor = e.target.id;
}
})
})
If you want me to code any project or post any specific post, feel free to DM me at IG @code.scientist or @codingtorque
If you have any doubts or any project ideas feel free to Contact Us
Hope you find this post helpful💖
Follow us on Instagram for more projects like this👨💻