0

On the table I created, there is an anchor tag embedded with a cross icon and I want to assign an event to it, however when I click it nothing happen. I have tried various way including $(".remove1"), $("#productTable tbody tr td a"),... Then I try to debug in chrome by setting up breakpoint but it just won't go into the function. the table

<table class="table" id="productTable">
    <thead class="thead-primary">
        <tr class="text-center">
            <th>&nbsp;</th>
            <th>&nbsp;</th>
            <th>Product name</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Total</th>
        </tr>
    </thead>
    <tbody>
        <tr class="text-center">
            <td class="product-remove"><a href="#/"><span><i class="ion-ios-close remove1"></i></span></a></td>
            <td class="image-prod">
                <div class="img" style="background-image:url(images/beef.jpg);"></div>
            </td>
            <td class="product-name">
                <h3>Beef Noodles Soup</h3>
            </td>
            <td class="price">RM12.00</td>
            <td class="quantity">
                <div class="input-group mb-3">
                    <button class="quantity-left-minus">-</button>
                    <input type="text" name="quantity" class="quantity form-control input-number" value="2" min="1" max="100">
                    <button class="quantity-right-plus">+</button>
                </div>
            </td>
            <td class="total">RM24.00</td>
        </tr>
        <tr class="text-center">
            <td class="product-remove"><a href="#/"><span><i class="ion-ios-close remove1"></i></span></a></td>
            <td class="image-prod">
                <div class="img" style="background-image:url(images/dumpling.jfif);"></div>
            </td>
            <td class="product-name">
                <h3>Dumpling</h3>
            </td>
            <td class="price">RM5.00</td>
            <td class="quantity">
                <div class="input-group mb-3">
                    <button class="quantity-left-minus">-</button>
                    <input type="text" name="quantity" class="quantity form-control input-number" value="1" min="1" max="100">
                    <button class="quantity-right-plus">+</button>
                </div>
            </td>
            <td class="total">RM5.00</td>
        </tr>
    </tbody>
</table>

jquery

$(function() {
    $(".product-remove a").on("click", function() {
        console.log("test");
    });
});

when i click on the a nothing written out in console, please help.

1 Answers1

0

Plain JS:

const icons = document.querySelectorAll('.product-remove a');

icons.forEach(element => {
  element.addEventListener("click", function(e){
    e.preventDefault();
    console.log("yeah");
  })
});

You need to give the anchor tag some sort of clickable area though:

.product-remove a{
  display: block;
  padding: 8px;
  background: goldenrod;
}
Supportic
  • 119
  • 1
  • 2
  • 12