How to append input data attribute to a button securely with Javascript or JQuery

7 Views Asked by At

I am trying to get the data from an input of the type number field and set its value to the data-quantity attribute on a button in the same parent div. I understand how to get the value from the Quantity field but I am not sure how to append it to the data-attribute of the button. This will be used for adding item into cart, so there will be more than 1 item.

Here's my code and what I tried:

<div class="row">
    <div class="col s12 m6">
        <input class="qtyBox" type="number" name="productQty" value="1" min="1" max="3">
    </div>
    <div class="col s12 m6">
        <a data-shop="<?= $product['s_id'] ?>" data-prod="<?= $product['product_id']  ?>" data-quantity="3" class="modal-trigger waves-effect btn-small orange darken-2 text-normal action-btn right addToCart">Add to Cart</a>
    </div>
</div>


$('.qtyBox').on('change', function({
    $(this).next('a').attr('data-quantity', $(this).val());
});
0

There are 0 best solutions below