Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what am I missing here? what I want is that when you click on the add to chart button that it will show the title of the product in the console (as a test) but I can't seem to ge it to work. I need to be able to click on the add to cart button and when I do that I want to shop the title of the cart-item in the console.log

this is my error:
winkel.js:46 Uncaught TypeError: Cannot read properties of undefined (reading 'innerText')
    at HTMLButtonElement.addToCartClicked (winkel.js:46)


this is the code for my products in html/php
while($row = $result-> fetch_assoc()){
            //echo $row['broodnaam'];
            echo '        <section class="py-5">
            <div class="shop-item">
            <div class="container px-4 px-lg-5 mt-5">
                <div class="row gx-4 gx-lg-5 row-cols-2 row-cols-md-3 row-cols-xl-4 justify-content-center">
                    <div class="col mb-5">
                        <div class="card h-100">
                            <!-- Product image dit kan later nog wel-->
                            <img class="card-img-top" src="https://dummyimage.com/450x300/dee2e6/6c757d.jpg" alt="..." />
                            <!-- Product details-->
                            <div class="card-body p-4">
                                <div class="text-center">
                                    <!-- Product name-->
                                    <div class="title">
                                        <h5 class="fw-bolder">'.$row['broodnaam']. '</h5>
                                    </div>
                                    <!-- Product price-->
                                    Prijs: €'.$row['prijs']. '<br>
                                    <!--voorraad--> 
                                    Voorraad: ' .$row['voorraad'].'
                                </div>
                            </div>
                            <!-- Product actions-->
                            <div class="shop-item-button">
                                <div class="card-footer p-4 pt-0 border-top-0 bg-transparent">
                                    <div class="text-center"><button class="btn btn-outline-dark mt-auto shop-item-button" type="button">Add to cart</button></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        </section>';
        }


javascript:
function ready() {
    var removeCartItemButtons = document.getElementsByClassName('btn-danger')
    console.log(removeCartItemButtons)
    //loop door alle knoppen in the cart
    for (var i = 0; i < removeCartItemButtons.length; i++) {
        var button = removeCartItemButtons[i]
        button.addEventListener('click', removeCartItem)
    }

    var quantityInputs = document.getElementsByClassName('cart-quantity-input')
    for (var i = 0; i < quantityInputs.length; i++) {
        var input = quantityInputs[i]
        input.addEventListener('change', quantityChanged)
    }

    var addToCartButtons = document.getElementsByClassName('shop-item-button')
    for (var i = 0; i < addToCartButtons.length; i++) {
        var button = addToCartButtons[i]
        button.addEventListener('click', addToCartClicked)
    }
}
function removeCartItem(event) {
    var buttonClicked = event.target
    buttonClicked.parentElement.parentElement.remove()
    updateCartTotal()
}

function quantityChanged(event) {
    var input = event.target
    if (isNaN(input.value) || input.value <= 0) {
        input.value = 1
    }
    updateCartTotal()

}

function addToCartClicked(event) {
    var button = event.target
    var shopItem = button.parentElement.parentElement
    var title = shopItem.getElementsByClassName('title')[0].innerText
    console.log(title)

}


What I have tried:

been looking around on the internet
Posted
Updated 13-Dec-21 1:51am
v4
Comments
Richard Deeming 13-Dec-21 7:42am    
"I can't get it to work" is not a question. You need to describe precisely what the problem is, what you have tried, and where you are stuck.
Rebecca2002 13-Dec-21 7:46am    
I need to be able to click on the add to cart button and when I do that I want the title of the cart-item in the console.log
Richard MacCutchan 13-Dec-21 7:54am    
It is an undefined reference, as noted in the error message. And the line:
var title = shopItem.getElementsByClassName('title')[0].innerText

is the offending one. So you need to find out why shopItem.getElementsByClassName('title')[0] does not return a valid object reference.

BTW I notice that none of your statements appear to be correctly terminated. Is this deliberate, or has something been lost in transcribing from your source to this question?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900