1

I created a form with fields name, price, count and the Add button. When clicking on this button, the product should be added to the array of products of the "Shop" class. But this does not happen and tell me whether I'm building the code structure correctly ?

class Product {
    constructor() {
        this.name = document.getElementById("name").value;
        this.count = document.getElementById("price").value;
        this.price = document.getElementById("count").value;
    }

}
class Shop {
    constructor() {
        this.products = [];

    }

    //method for adding a product
    addProduct(newProduct) {
        this.products.push(newProduct);
    }
}
const shop = new Shop();
const buttAdd = document.getElementById("add");
buttAdd.addEventListener("click", (e) => {
    shop.addProduct(new Product(this.name, this.count, this.price));

}, false);
console.log(shop.products);
   <form>
            <label for="name" >Name of product</label>
            <input type="text"  id="name" class="input-product">
            <label for="price">Price of product</label>
            <input type="text"  id="price" class="input-product">
            <label for="count">Count of product</label>
            <input type="text"  id="count" class="input-product">
            <button id="add">Add</button>
        </form>

3
  • What happens if you also give name="name|price|count"attribute for input fields. Commented Jul 8, 2018 at 10:08
  • I just change the id to a name, it does not change the logic Commented Jul 8, 2018 at 10:14
  • You have an answer but my comment was adding a name attribute, not to replace existing id attribute. Commented Jul 8, 2018 at 11:23

1 Answer 1

1

There is no issue in your code. It's just that your print statement should be inside onclick event. You should write your asynchronous prints within callbacks in JS

class Product {
    constructor() {
        this.name = document.getElementById("name").value;
        this.count = document.getElementById("price").value;
        this.price = document.getElementById("count").value;
    }

}
class Shop {
    constructor() {
        this.products = [];

    }

    //method for adding a product
    addProduct(newProduct) {
        this.products.push(newProduct);
    }
}
const shop = new Shop();
const buttAdd = document.getElementById("add");
buttAdd.addEventListener("click", (e) => {
    shop.addProduct(new Product(this.name, this.count, this.price));
   console.log(shop.products);
}, false);
   <form>
            <label for="name" >Name of product</label>
            <input type="text"  id="name" class="input-product">
            <label for="price">Price of product</label>
            <input type="text"  id="price" class="input-product">
            <label for="count">Count of product</label>
            <input type="text"  id="count" class="input-product">
            <button id="add">Add</button>
        </form>

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

Comments

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.