How to identify changed value in input

3

How to identify if a setting value has changed in input ? I have the prototype below, however, the alert will be displayed according to the number of times the class="ultimo-valor" appears in the code.

<input class="ultimo-valor" onKeyUp="ultimoValor(this)" type="text" 
value="<?php echo $ultimo_valor; ?>"/>

<input class="ultimo-valor" onKeyUp="ultimoValor(this)" type="text" 
value="<?php echo $ultimo_valor; ?>"/>

Script:

<script>
$(document).ready(
   function ultimoValor(e) {
      $('.ultimo-valor').on("change paste keyup", function() {
       alert($(this).val());
      });
});
</script>
    
asked by anonymous 20.01.2017 / 23:02

2 answers

1

I'm assuming you're looking for a response that works with javascript pure or with jquery .... (are the tags referenced in the question)

var ultimovalor = 8;

function verifica_valor(element) {
  if (ultimovalor != element.value) {
    alert("Alterou!\nNovo valor: "+element.value); 
    ultimovalor = element.value;
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><inputonchange="verifica_valor(this)" type="text" value="20" />

<input onchange="verifica_valor(this)" type="text" value="10" />
    
20.01.2017 / 23:28
2

Would it be so? When loading the document, I save the value of the field in the variable valor and when changing the field, I check if it is different from the value that is stored in the variable. If this is different I send alert and I change the value of the variable valor that contains the current value of the field.

See working:

$(document).ready(function() {
  //variavel que recebe o valor atual ao carregar a pagina
  var valor = $(this).val();
  // funcao que é executada ao carregar a pagina
  $('.ultimo-valor').on("change paste keyup", function() {
    // verifica se o valor esta diferente do campo
    if (valor != $(this).val()) {
      // aqui o valor foi alterado
      alert($(this).val());
      //atualiza o valor da variavel
      valor = $(this).val();
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputclass="ultimo-valor" type="text" value="" />
<input class="ultimo-valor" type="text" value="" />
  
    
20.01.2017 / 23:28