How to identify changed value in input

Asked

Viewed 1,054 times

4

How to identify if a determining value has been changed in input? I have the prototype below, however 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>
  • Are you making a mistake? It’s not working?

  • http://jsfiddle.net/teuwLswp/

  • @Localhost, the alert should be displayed only once for each change.

  • published an answer, see if that’s it

  • @Localhost, thanks for the help. I don’t know if I explained the problem clearly, but Marcelobonifazio answered correctly.

  • I believe that the answers were not so different. But I’m glad you got the expected result :)

  • Can you change it to more than one input?

  • Yes... edited there, I had withdrawn just to get better visualizing...

Show 3 more comments

2 answers

3

Would that be so? When loading the document, save the value of the field in the variable valor and when making the field change, I check if it is different from the value stored in the variave. If this different I send the alert and change the value of the variable valorthat 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>
<input class="ultimo-valor" type="text" value="" />
<input class="ultimo-valor" type="text" value="" />
  

  • [+1]. Grateful for the alternative.

2


I’m assuming you’re looking for an answer 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>
<input onchange="verifica_valor(this)" type="text" value="20" />

<input onchange="verifica_valor(this)" type="text" value="10" />

  • Marcelo thanks for the reply. AH, JavaScript is a language and jQuery is a library of JavaScript.

  • @luccasrodrigo yes I know, I think I didn’t express myself well :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.