1

I have been trying to write a code to create a simple increment and decrement counter in html using javascript and i am not able figure out what`s going wrong! i'm attaching the snippets here, please help me if you can!

var i=0;
const plus = function(){
    i += 1;
    document.getElementById("demo").innerText = i;
}
const minus = function(){
    i += 1;
    document.getElementById("demo").innerText = i;
}
.container{
height: 400px;
width: 400px;
background-color: rgb(240, 168, 221);
}
.container2{
    text-align: center;
}
.center {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
  }
<!doctype html>
<html>
    <head>
        <title> interactiveCart.io </title>
        <link rel="stylesheet" href="app.css">
        <script src="https://kit.fontawesome.com/ecf5b6c191.js" crossorigin="anonymous"></script>
    </head>
    <body>
        <h1> Interactive Cart For Ecommerce Store </h1>
        <div class="container">
            <img class="center" src="shopping-cart.png" alt="loading" height="300px" width="300px" >
        </br>
        <div class="container2">
            <script src="app.js"></script>
            <button class="button1" onclick="plus();"> Add <i class="fa-solid fa-circle-plus"></i> 
            </button>
            <button class="button2" onclick="minus();"> Remove <i class="fa-solid fa-circle-minus"></i> 
            </button>
            </div> </div>
    </body>
</html>

3
  • You have no item with the id "demo" Commented Apr 19, 2022 at 12:33
  • 2
    ... and both the functions are doing the same math. Commented Apr 19, 2022 at 12:34
  • Update minus function with i -= 1; and add a span or div in your code with id=demo like this <span id="demo"></span> Commented Apr 19, 2022 at 12:48

2 Answers 2

1

You need to add element with id "demo"

And change the minus function with -= operator.

var i=0;
const plus = function(){
    i += 1;
    document.getElementById("demo").innerText = i;
}
const minus = function(){
    i -= 1;
    document.getElementById("demo").innerText = i;
}
.container{
height: 400px;
width: 400px;
background-color: rgb(240, 168, 221);
}
.container2{
    text-align: center;
}
.center {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
  }
<!doctype html>
<html>
    <head>
        <title> interactiveCart.io </title>
        <link rel="stylesheet" href="app.css">
        <script src="https://kit.fontawesome.com/ecf5b6c191.js" crossorigin="anonymous"></script>
    </head>
    <body>
        <h1> Interactive Cart For Ecommerce Store </h1>
        <div class="container">
            <img class="center" src="shopping-cart.png" alt="loading" height="300px" width="300px" >
            <span id="demo"></span>
        </br>
        <div class="container2">
            <script src="app.js"></script>
            <button class="button1" onclick="plus();"> Add <i class="fa-solid fa-circle-plus"></i> 
            </button>
            <button class="button2" onclick="minus();"> Remove <i class="fa-solid fa-circle-minus"></i> 
            </button>
            </div> </div>
    </body>
</html>

Sign up to request clarification or add additional context in comments.

Comments

0

here i've adding a div with id=demo

var i=0;
const plus = function(){
    i += 1;
    document.getElementById("demo").innerHTML = i;
}
const minus = function(){
    i -= 1;
    document.getElementById("demo").innerHTML = i;
}
.container{
height: 400px;
width: 400px;
background-color: rgb(240, 168, 221);
}
.container2{
    text-align: center;
}
.center {
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
  }

<!doctype html>
<html>
    <head>
        <title> interactiveCart.io </title>
        <link rel="stylesheet" href="app.css">
        <script src="https://kit.fontawesome.com/ecf5b6c191.js" crossorigin="anonymous"></script>
    </head>
    <body>
        <h1> Interactive Cart For Ecommerce Store </h1>
        <div class="container">
            <img class="center" src="shopping-cart.png" alt="loading" height="300px" width="300px" >
        </br>
        <div class="container2">
            <script src="app.js"></script>
            <button class="button1" onclick="plus();"> Add <i class="fa-solid fa-circle-plus"></i> 
            </button>
            <button class="button2" onclick="minus();"> Remove <i class="fa-solid fa-circle-minus"></i> 
            </button>
            <div id="demo"> </div>
         </div>
    </body>
</html>

1 Comment

Thank you so much, i figured it out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.