Cancel input event

0

I have several input in a table in which only numbers are accepted, I have created a function to verify that they are greater than zero or that they are different from empty after losing the focus, the javascript code I use for that control is:

function cambiaCantidad(){
if (document.getElementById("tabProd").rows[indice_fila_tabla].cells[3].getElementsByTagName('input')[0].value!=='') {
    var valorCelda=document.getElementById("tabProd").rows[indice_fila_tabla].cells[3].getElementsByTagName('input')[0].value;
    if (valorCelda>0){
    cambiaValores();
}
    else{
        alert("La cantidad tiene que ser mayor a cero");
        document.getElementById("tabProd").rows[indice_fila_tabla].cells[3].getElementsByTagName('input')[0].value=1;
        cambiaValores();
    }
}
else{
    alert("La cantidad es requerida");
        document.getElementById("tabProd").rows[indice_fila_tabla].cells[3].getElementsByTagName('input')[0].value=1;
        cambiaValores();
}

}

My question is: If the input has a value and then I change it to zero or empty, how can I get it back to the previous value I had, that is, if I had the number 1 and then I change it to zero so that it returns to the previous value (one)

    
asked by Leo T 07.03.2018 в 17:04
source

2 answers

1

You can save the previous value in an input attribute, with a code like this:

function verificarValor(input){
	if(input.value == "" || parseInt(input.value) <= 0){
		input.value = input.getAttribute("valorAnterior");
	}else{
		input.setAttribute("valorAnterior",input.value);
	}
}
<input onchange="verificarValor(this);" valorAnterior="1" value="">
    
answered by 07.03.2018 / 18:06
source
0

Save the value in a variable, if it is a lot of values you could use an array.

Here is a simple example using "click" events

//Inicializar
var valorAnterior = 0;

//Agregar monitores de eventos
document.getElementById('botonG').addEventListener('click', guardar);
document.getElementById('botonD').addEventListener('click', deshacer);

//Funciones de eventos

// Guarda el valor en una variable global
function guardar() {
  valorAnterior = document.getElementById('entrada').value;
  console.info(valorAnterior);
}

// Deshace el cambio realizado, es decir, asigna el valor en la variable global que fue guardado.
function deshacer() {
  document.getElementById('entrada').value = valorAnterior;
}
<input id="entrada" type="number">
<input id="botonG" type="button" value="Guardar">
<input id="botonD" type="button" value="Deshacer">

Then a second example that instead of using buttons uses the input event.

// Inicializar
var entrada = document.getElementById('entrada');
var valorAnterior = 0;

// Agrevar monitor de cambios de elementos input
entrada.addEventListener('input', control);

// En caso de que se ingrese una cadena vacía o un 0 se coloca el valor anterior
function control() {
  var valor = entrada.value;
  if (valor == "") {
    entrada.value = valorAnterior;
    console.info('Se agregó valor anterior');
  }
  valorAnterior = entrada.value;
}
<input id="entrada" type="number">
    
answered by 07.03.2018 в 17:22