I know that to change the value of a label it is valid with html () or text () but the problem is that it does not work. Let me explain:
The function of the program is that by double clicking on an element with the class ".item" that element is cloned and added to a div, that the stock is subtracted in one and that the price and number of items increase.
I define $ stockLblInicial and $ cantStockInicial when loading the page to check if there is stock and if there is a double click, if there is nothing then.
Loading the page:
$(function(){
$("#citem").val(0); //Ponemos el contador de items a cero cada vez que se refresque la pagina
$("#cprice").val(0 + " €"); //Ponemos el valor del precio total a cero cada ves que se refresque la pagina
var $stockLblInicial = $(".stock", $(this)).text(); //Pillamos la etiqueta del stock
var $cantStockInicial = parseInt($stockLblInicial.slice(6)); //Cogemos solo el numero y lo pasamos a int
//Hago un slice ya que el label es tal que "Stock 10" y solo quiero el numero
//Si hay items en stock
if($cantStockInicial > 0){
$(".item").on("dblclick", dobleClickItems);
}else{
return false;
}
});
Within the doubleClick function we define $ stockLbl and $ cantStock because later the value of these variables will be trampled. In the function itself, it takes those values so that in purchase () the stock is subtracted in one and assigned to the text of the clicked item. The text does not change then it always catches like there are 10 items. The problem is not in the counter because it remains as it touches. The problem is directly does not change the text. I have tried with text () and with html () and it does not work.
function dobleClickItems(){
//Variable necesarias para clonar
const $divCarrito = $("#cart_items"); //Pillamos el contenedor del carrito
let $idItem = $(this).attr("id"); //Pillamos la id del item seleccionado (devuelve un string)
let $clonedItem = $(this).clone(); //Clonamos el item
var $stockLbl = $(".stock", $(this)).text(); //Pillamos la etiqueta del stock
var $cantStock = parseInt($stockLbl.slice(6)); //Cogemos solo el numero y lo pasamos a int
let $precioLabel = $(".price", $(this)).text(); //Pillamos el precio del producto clickado
let $precio = parseInt($precioLabel.slice(0, 4)); //Cogemos el numero y lo pasamos a int
let $numItems = $("#citem").val(); //Pillamos el valor del input de la cantidad de elementos
parseInt($numItems); //Pasamos el valor a int para poder hacer operaciones
//Pillamos el valor del input del precio total y lo pasamos a int tambien
let $precioTotal = parseInt($("#cprice").val().slice(0, 4));
compra($cantStock, $numItems, $precioTotal, $precio);
cloneItem($idItem, $clonedItem, $divCarrito);
if($cantStock == 0){ //Si no quedan items le añadimos la clase "agotado" al item
$(".stock", $(this)).addClass("agotado");
}
}
Here the function buys:
function compra($cantStock, $numItems, $precioTotal, $precio){
$cantStock--; //Contador para decrementar el valor del stock
$(".stock", $(this)).html("Stock " + $cantStock); //Reducimos en uno el numero de stock
console.log("Cantidad en stock " + $cantStock);
$numItems++; //Incrementamos el numero de items comprados
$("#citem").val($numItems); //Aumentamos el numero de compras
console.log($numItems);
$precioTotal += $precio; //Al precio del input le vamos sumando el precio de todas la compras
$("#cprice").val($precioTotal + " €");
}
The console does not throw any problems, it's just that the label does not change it and I do not know why it can be. If someone could help me, I would be very grateful.
The html:
<!DOCTYPE html>
<html>
<head>
<title>Carro de la compra con jQuery</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" title="normal" href="css/carro.css" type="text/css" media="screen" />
<!-- CDN JQuery de Google -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.0.min.js"
type="text/javascript"></script>
<script src="scripts/carro.js" type="text/javascript"></script>
</head>
<body>
<div id="item_container">
<div class="item" id="i1">
<img src="img/camiseta1.jpg" alt="descripción i1"/>
<label class="title">Camiseta 1</label>
<label class="price">20 €</label>
<label class="stock">Stock 10</label>
</div>
<div class="item" id="i2">
<img src="img/reloj2.jpg" alt="descripción i2"/>
<label class="title">Reloj 2</label>
<label class="price">24 €</label>
<label class="stock">Stock 10</label>
</div>
<div class="clear"></div>
</div>
<div id="cart_container">
<div id="cart_title">
<span>Carrito</span>
<div class="clear"></div>
</div>
<div id="cart_toolbar">
<div id="cart_items" class="back"></div>
</div>
<div id="navigate">
<div id="nav_left">
<button id="btn_comprar" title="Confirma la compra de los artículos">Comprar</button>
<button id="btn_prev" title="Desplaza el carrito hacia la izquierda"><</button>
<button id="btn_next" title="Desplaza el carrito hacia la derecha">></button>
<button id="btn_clear" title="Vacia el carrito">Vaciar</button>
</div>
<div id="nav_right">
<span class="sptext">
<label>Compras </label><input id="citem" value="0" readonly title="Número de productos comprados"/>
</span>
<span class="sptext">
<label>Precio </label><input id="cprice" value="0 €" readonly title="Precio total de los productos comprados"/>
</span>
</div>
<div class="clear"></div>
</div>
</div>
</body>
</html>
PS: If it were necessary the html I edit the question and I add it also.
EDIT: The error specifically is that I try to edit the label with class "stock" inside the div with class "item" but it does not. This I specifically do here: $(".stock", $(this)).html("Stock " + $cantStock);
It is the second line of the purchase () function.
I hope this clarifies it a bit more.
EDIT 2: I want to modify the label of the element that I click twice because of what the variable $ this is when I define $ stockLbl. What I want is to change the content of that label and access it through its ".stock" class. Specifically, I want to double click the number of the label is reduced by one. That is why I have a variable $ cantStock that I use as a counter and that is initialized when I make a split of the content of the label until I only have the number.
Equal seeing the layout as it is is understood a little better:
When you double click on one of the containers where the product is, the stock number should be reduced by 1.