Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nurularifin's avatar

How to Make Button Only Sent Value Base On Button Row I Click

Hi Everyone, I really need help, I have a problem and struggling to solve it, I have a table displaying all detailed transactions I'm actually making POS transactions, in this table, there is a QUANTITY column where I put INPUT BUTTON PLUS MINUS, but the problem, if I click one of the rows for BUTTON other row INPUT follow too, how to make that only sent value for the button's row I click. The following is my code:

My HMTL Code:

<button type="button" id="minus_' + value.id + '" class="quantity-left-minus btn btn-danger btn-sm btn-number" data-type="minus" data-field="quantity" data-quantity="' + value.id +'" style="padding: 0 5px">-</button>
<input type="text" id="quantity" name="quantity" class="input-number" value="1" min="1" max="100" style="width: 100%;min-width: 30px;margin-top: 5px; height: 30px">

My Jquery Code

 // Quantity minus
        $(document).on('click', 'button[id^="minus_"]', function (e) {
            e.preventDefault();

            var minus = $(this).attr('data-quantity');

            // Get the field name
            fieldName = $(this).attr('data-field');
            // Get its current value
            var currentVal = parseInt($('input[name='+fieldName+']').val());
            // If it isn't undefined or its greater than 0
            if (!isNaN(currentVal) && currentVal > 0) {
                // Decrement one
                $('input[name='+fieldName+']').val(currentVal - 1);
            } else {
                // Otherwise put a 0 there
                $('input[name='+fieldName+']').val(0);
            }
        });
0 likes
1 reply
jameswise's avatar

There are a few ways to fix this problem. You could add an id to each row and then use that id to target the specific row that was clicked. Alternatively, you could use event delegation and listen for clicks on the button elements. When a button is clicked, you can then use the event data to determine which row was clicked and take the appropriate action.

Please or to participate in this conversation.