0
I am creating an Ecommerce site within this site I have Divs with products where I have buttons to change input values but I have to replicate functions in javascript and change Divs class names every time. This is the problem if I have 100 products I will need to replicate 100 times. Is there any way to do this without replicating for each product? I’ll post here the part of the code I need help with.
Part of the Product
<span>Preço de Produto:</span>
<span class="valor" id="Valor">200</span>€<br>
<br>
<input type="image" src="https://liedobom.pt/wp-content/uploads/2018/04/1-5.jpg" alt="" width="100" height="100">
<br>
<input id="Menos" type="button" onclick="Menos();Resultado()" value="-">
<input type="number" name="Qtd" id="Qtd" maxlength="4" value="0" size="4">
<input id="Mais" type="button" onclick="Mais();Resultado()" value="+"><br><br>
<span>Total</span><br>
<div class="Ajudante">
<input type="number" name="" id="PPTTL" value="0" readonly><br><br>
</div>
<input type="button" onclick="Exibir()" value="Add Carrinho"><br><br>
<input type="button" value="Comprar">
Javascript
//Produto 1///
function Menos(){
var Qtd = document.getElementById("Qtd").value;
document.getElementById("Qtd").value = Qtd - 1;
if(document.getElementById("Qtd").value <= 0){
document.getElementById("Qtd").value = 0;
}
}
function Mais(){
var Qtd = document.getElementById("Qtd").value;
mais = 1;
document.getElementById("Qtd").value = Qtd +++ mais;
if(document.getElementById("Qtd").value <= 0){
document.getElementById("Qtd").value = 0;
}
}
function Resultado(){
var Qtd = document.getElementById("Qtd").value;
var Vlr = document.getElementById("Valor").innerHTML;
document.getElementById("PPTTL").value = Qtd * parseInt(Vlr);
}
This site is in Portuguese, so translate your question.
– Filipe L. Constante
Thank you so much for the tip ;)
– Jessé Mendonça
Instead of searching for items by ID, searching for the class and then Cascading to get the item you need.
– José Francisco
You can add your button click event to js. When the button is clicked you have the HTML element reference, then you can browse and find the values you need. Look that.
– DaviAragao