How to create a counter
- Web Coding

 - Sep 6, 2020
 - 1 min read
 
Learn how to create a counter with HTML, CSS and JavaScript
HTML and CSS
<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=
    scr, initial-scale=1.0">
 <title>Document</title>
 <style>
 body{
 min-height: 100vh;
 display: flex;
 align-items: center;
 justify-content: center;
        }
 </style>
</head>
<body>
 <div>
 <p>Counter: <span id="counterValue">0</span></p>
 <button id="subtract">Subtract</button>
 <button id="add">Add</button>
 <script src="script.js"></script>
 </div>
 
</body>
</html>JavaScript
let add = document.getElementById('add');
let subtract = document.getElementById('subtract');
let counterValue = document.getElementById('counterValue');
add.addEventListener('click', addFunction);
subtract.addEventListener('click', subtractFunction);
function addFunction(){
 counterValue.innerHTML = ((parseFloat(counterValue.innerHTML) + 1));
}
function subtractFunction(){
 counterValue.innerHTML = ((parseFloat(counterValue.innerHTML) - 1));
}If you found this blog post helpful don’t forget to check out my other projects and if you want more content follow me on
YouTube: https://www.youtube.com/channel/UC2nxqB2usQIS2c8zwVZKWEQ
Instagram: https://www.instagram.com/coding_for_the_web/
Twitter: https://twitter.com/LearnWebCoding



Comments