skip to Main Content

ISSUE: Trying to create a people counter, where every time Increment button is clicked the count should increase by 1. But when I m clicking Increment button nothing is happening. Please help to fix the issue

JS code

code:

HTML code

2

Answers


  1. Try this:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>People Counter</title>
    </head>
    <body>
    
    <h1>People Counter</h1>
    
    <p>Count: <span id="count">0</span></p>
    <button onclick="incrementCount()">Increment</button>
    
    <script>
        // JavaScript code
        let count = 0;
    
        function incrementCount() {
            count++;
            document.getElementById('count').innerText = count;
        }
    </script>
    
    </body>
    </html>
    
    Login or Signup to reply.
  2. You have to call increment() function in the button click. Also move

    let countE1 = document.getElementById("count-e1");
    

    inside the increment() function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search