Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error in creating Increment Decrement Counter in HTML Using JavaSCript

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>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading