DEV Community

Cover image for 🟨 JavaScript 3 | **Simple Counter**
Gregor Schafroth
Gregor Schafroth

Posted on

🟨 JavaScript 3 | **Simple Counter**

New day new JavaScript beginner Exercise! Here is what I got from ChatGPT today, and my solution below as always 😊

Create a Simple Counter

Objective: Build a web page with a button that, when clicked, increments a counter. This exercise will help you practice handling user events (like button clicks) and updating the webpage dynamically.

Steps:

  1. HTML Setup: Create an HTML file with a button and a placeholder to display the counter's value.
  2. Initialize the Counter: In your JavaScript, start with a variable set to zero. This will keep track of the counter's value.
  3. Increase Counter on Button Click: Add an event listener to the button so that each time it's clicked, the counter variable increases by 1.
  4. Display the Counter: Update the counter display on the webpage each time the button is clicked.
  5. Styling (Optional): Add some basic CSS to make your button and counter display look nice.

My Code

Alright I am starting to get annoyed that I can’t remember the JavaScript syntax out of my head, because I was mostly able to do that with Python. But I guess that’s exactly why python is getting so popular: It’s so easy and obvious

Well anyways that doesn’t help me here. Instead I decided to recreate todays code so often until I can just write it on my own without any help of ChatGPT. Took me 4 attempts until I finally was able to do even tho it is so simple, phewww

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Simple Counter 4</title>
</head>
<body>
    <h1 id="countDisplay">0</h1>
    <button id="countButton">Count</button>
    <script>
        let countDisplay = document.getElementById('countDisplay');
        let countButton = document.getElementById('countButton');
        let counter = 0;

        function incrementCounter() {
            counter++;
            countDisplay.textContent = counter;
        }

        countButton.addEventListener('click', incrementCounter)

    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Heroku

Amplify your impact where it matters most — building exceptional apps.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup 🚀

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay