0
Good morning, I’m stuck in an issue in my code where I have several buttons and inputs type "number" on the same page and with the click of the button, adds x times (input value) the specific product in the shopping cart.
<input type="number" class="quantidade" value="1" data-quantity="{{ product.id }}">
<input type="button" id="{{ product.id }}" value="Comprar" onclick="addCart()">
I thought of creating a "data-Quantity" attribute in input with the product id I get via Twig and compare it with the button id which is the product id itself. However, how do I do this verification via Javascript now? I’m trying so.
function addCart(product_id){
var botao = $('#{{ product.id }}');
var entrada = $('.quantidade');
if (botao.attr('id') == entrada.attr('data-quantity')) {
var quantidade = entrada.val();
console.log(quantidade); //Deve imprimir a quantidade do input referente ao botão apertado.
// Aqui eu envio o valor da variável quantidade via ajax, o que não é problema depois que conseguir armazenar esse valor.
}
}
Put in the question the HTML of where the field is and the button that can be done in a much better and simpler way.
– Sam