Send form with onClick. Only Javascript and HTML

2

I'm doing the shopping cart part of a page (It's a college job, I'm just starting). For each item in the cart you should be able to increase and decrease the quantity purchased. I managed to do it but only with one product, if I copy and paste several times it stops working completely. I think it's because having all the same name does not know which to increase or decrease and that's why it does not work. I have this code:

<div class="cantidadProducto">
     <p>Cantidad</p>
     <form name="formCantidad">
           <a onclick="aumentarUno(formCantidad)"><i class="fas fa-angle-up"></i></a>
           <input type="text" name="numero" class="numeroCantidadProducto" value="1" ReadOnly>
           <a onclick="disminuirUno(formCantidad)"><i class="fas fa-angle-down"></i></a>
     </form>
</div>

I want to send the form with the name="formCantidad", but since that will be repeated in each product purchased, I only want to send the one for that article. How could I do? Try using the this.formQuantity but it does not work. For what it's worth, I leave the Javascript function

function aumentarUno(form)
{
    if(form.numero.value < 10){
        form.numero.value ++;
    }else{
        alert("La cantidad maxima de productos a comprar es 10");
    }
}
    
asked by enan24 22.06.2018 в 00:55
source

2 answers

3

It's actually inefficient to call aumentarUno and disminuirUno passing it the name of form . What you should do is pass the reference of the element that called it:

<a onclick="aumentarUno(this)"><i class="fas fa-angle-up"></i></a>

The function receives the a element and can infer what form it is by asking the parentNode of the element. Knowing the parentNode, you proceed in the way you already had implemented.

This way you save yourself having to give a unique name to each form and modify the code of each of the links. Only copies and sticks to infinity.

function aumentarUno(element)
{
    var form = element.parentNode;
    if(form.numero.value < 10){
        form.numero.value ++;
    }else{
        alert("La cantidad maxima de productos a comprar es 10");
    }
}

function disminuirUno(element)
{
    var form = element.parentNode;
    if(form.numero.value > 0){
        form.numero.value--;
    }else{
        alert("Su carrito ya está en cero");
    }
}
form {
margin-bottom: 3px;
}
form span {
display:inline-block;
width:100px;
}
<link rel="stylesheet"
          href="https://use.fontawesome.com/releases/v5.0.13/css/all.css"
          integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp"
          crossorigin="anonymous">
<div class="cantidadProducto">
     <p>Cantidad</p>
     <form name="formCantidad">
           <span>Pan</span>
           <a onclick="aumentarUno(this)"><i class="fas fa-angle-up"></i></a>
           <input type="text" name="numero" class="numeroCantidadProducto" value="1" ReadOnly>
           <a onclick="disminuirUno(this)"><i class="fas fa-angle-down"></i></a>
     </form>
     <form name="formCantidad">
           <span>Leche</span>
           <a onclick="aumentarUno(this)"><i class="fas fa-angle-up"></i></a>
           <input type="text" name="numero" class="numeroCantidadProducto" value="1" ReadOnly>
           <a onclick="disminuirUno(this)"><i class="fas fa-angle-down"></i></a>
     </form>
     <form name="formCantidad">
           <span>Huevos</span>
           <a onclick="aumentarUno(this)"><i class="fas fa-angle-up"></i></a>
           <input type="text" name="numero" class="numeroCantidadProducto" value="1" ReadOnly>
           <a onclick="disminuirUno(this)"><i class="fas fa-angle-down"></i></a>
     </form>
     <form name="formCantidad">
           <span>Harina</span>
           <a onclick="aumentarUno(this)"><i class="fas fa-angle-up"></i></a>
           <input type="text" name="numero" class="numeroCantidadProducto" value="1" ReadOnly>
           <a onclick="disminuirUno(this)"><i class="fas fa-angle-down"></i></a>
     </form>
</div>

Now, it is a bit impractical to have a form for each product. I imagine that when you want to persist the purchase you will want to send an object that represents each product and its respective quantity. This can be done with a little metaprogramming: you only draw the form (one only) and declare an array of products:

var productos=['pan','leche','huevos','harina'];

And for each of them, you dynamically create a fieldset that you add to the form. Within each field there is an input whose name is the product. That way when you submit the form you have N tuplas product-value:

var productos = ['pan', 'leche', 'huevos', 'harina'];

function aumentarUno(event) {
    var element = event.target,
        fieldset = element.parentNode,
        numero = fieldset.querySelector('.numeroCantidadProducto');
    if (numero.value < 10) {
        numero.value++;
    } else {
        alert("La cantidad maxima de productos a comprar es 10");
    }
}

function disminuirUno(event) {
    var element = event.target,
        fieldset = element.parentNode,
        numero = fieldset.querySelector('.numeroCantidadProducto');
    if (numero.value > 0) {
        numero.value--;
    } else {
        alert("Su carrito ya está en cero");
    }
}

function addFieldSet(producto) {

    var fieldset = document.createElement('fieldset'),
        span = document.createElement('span'),
        aumentar = document.createElement('a'),
        cantidad = document.createElement('input'),
        disminuir = document.createElement('a');

    span.innerText = producto;
    aumentar.onclick = aumentarUno;
    disminuir.onclick = disminuirUno;

    aumentar.className = 'fas fa-angle-up';
    disminuir.className = 'fas fa-angle-down';


    cantidad.type = "text";
    cantidad.name = producto;
    cantidad.className = 'numeroCantidadProducto';
    cantidad.value = 1;
    cantidad.readOnly = 'readonly';

    fieldset.appendChild(span);
    fieldset.appendChild(aumentar);
    fieldset.appendChild(cantidad);
    fieldset.appendChild(disminuir);

    document.getElementById('formCantidad').appendChild(fieldset);

}

function enviarCarrito() {
    document.getElementById('formCantidad').querySelectorAll('.numeroCantidadProducto').forEach(function (input) {
        console.log(input.name, input.value);
    });
}

productos.forEach(function (producto) {
    addFieldSet(producto);
});
form {
margin-bottom: 3px;
}
form span {
display:inline-block;
width:100px;
}
.cantidad {
width:150px;
float:left;
display:inline-block;
}
fieldset {
clear:both;
 border: 0 none;
}
.numeroCantidadProducto {
margin: 0 10px;
}

.enviar {
    float: left;
    margin-bottom:10px;
}
<link rel="stylesheet"
          href="https://use.fontawesome.com/releases/v5.0.13/css/all.css"
          integrity="sha384-DNOHZ68U8hZfKXOrtjWvjxusGo9WQnrNx2sqG0tfsghAvtVlRW3tvkXWZh58N9jp"
          crossorigin="anonymous">
<form name="formCantidad" id="formCantidad" class="cantidadProducto">
     <span class="cantidad">Cantidad</span>
     <input type="button" onclick="enviarCarrito()" class="enviar" value="enviar">
     
</form>
    
answered by 22.06.2018 / 01:18
source
1

If you have the ability to write HTML, you can number the forms to distinguish them from each other. Here is an example:

function aumentarUno(form)
{
    if(form.numero.value < 10){
        form.numero.value ++;
    }else{
        alert("La cantidad maxima de productos a comprar es 10");
    }
}
<div class="cantidadProducto">
     <p>Cantidad</p>
     <form name="formCantidad">
           <a onclick="aumentarUno(formCantidad)"><i class="fas fa-angle-up"></i>up</a>
           <input type="text" name="numero" class="numeroCantidadProducto" value="1" ReadOnly>
           <a onclick="disminuirUno(formCantidad)"><i class="fas fa-angle-down"></i>down</a>
     </form>
</div>
<div class="cantidadProducto">
     <p>Cantidad</p>
     <form name="formCantidad1">
           <a onclick="aumentarUno(formCantidad1)"><i class="fas fa-angle-up"></i>up</a>
           <input type="text" name="numero" class="numeroCantidadProducto" value="1" ReadOnly>
           <a onclick="disminuirUno(formCantidad)"><i class="fas fa-angle-down"></i>down</a>
     </form>
</div>
    
answered by 22.06.2018 в 01:08