0
My doubt is a little confusing, but I hope to be as clear as possible. As shown in the image ( https://i.stack.Imgur.com/Lafjw.png ) I have a cart and the div of each element (product) has the same id, since it is the model and all other products will use the same structure. The problem is that when I add an item in the first product (rice) all products suffer the addition, since they use the same ID, and in javascript the command happens using the ID. Would it be possible to generate a different ID every time a new product is added to the cart? As if it were the primary key of a database.
HTML of one of the cart items:
<div class="container-fluid" id="modeloProdutos">
<div class="row">
<!-- descrição produtos + imagem -->
<div class="col-sm-12 col-md-4 col-lg-6">
<img src="img/arroz.jpg">
Arroz integral tipo C 5KG Arroz integral tipo C 5KG Arroz integral tipo C 5KG Arroz integral tipo C
</div>
<div class="col-sm-12 col-md-4 col-lg-2">
<!-- informar quantidade de produtos -->
<div class="container-fluid" id="divAddProdutosPagamentos">
<div class="row">
<!-- Botão de subtrair -->
<!-- nao consigo redimensionar o botao -->
<div class="input-group-prepend col-lg-3" id="buttonMinus">
<button class="btn btn-primary" type="button"><i class="fas fa-minus"></i></button>
</div>
<!-- Campo p/ informar a qtd-->
<!-- aqui vai setado a quantidade de itens informado no index -->
<input type="text" class="form-control input-group-prepend col-lg-3" id="inputQtd"
value="0">
<!-- Botão de adicionar -->
<div class="input-group-prepend col-lg-3" id="buttonAdd">
<button class="btn btn-primary" type="button"><i class="fas fa-plus"></i></button>
</div>
</div>
</div> <!-- FIM DA DIV ADD PRODUTO -->
</div>
<div class="col-sm-12 col-md-4 col-lg-2">
R$ 23.11
</div>
<div class="col-sm-12 col-md-4 col-lg-2">
R$ 43.44
</div>
</div>
</div>
Javascript:
$(document).ready(function(){
$('#buttonAdd').click (function(){
count++
$('#inputQtd').val(count);
$('#badgeCarrinho').text(count);
});
});
PS: badgeCart is a notification next to the cart icon in the upper right corner (where 4)
The same id is not used for more than one element. An id must be unique. Use class instead of id.
– Sam
It wouldn’t have the same effect
this
.– Sam
I understood what I meant, I believed that this worked for the ID too, forgive my ignorance.
– Weslley Fillipe