0

which is the correct way to pass element_old to the element.change function?

  $('body').on('click', 'input', function(){
      element = $(this);
      element_old = element.val();

      element.change( function(element_old){
        if($(this).val() != element_old){ show button}
        else{ hide button }           

      })

    });

I want to show a button only if the input value has changed from its original value.

Clorge32
  • 341
  • 1
  • 3
  • 7
  • You can't. `onchange` is an event handler which is called on the element which raised the event – Rory McCrossan Oct 04 '16 at 07:55
  • 1
    Possible duplicate [http://stackoverflow.com/questions/1909992/how-to-get-old-value-with-onchange-event-in-text-box](http://stackoverflow.com/questions/1909992/how-to-get-old-value-with-onchange-event-in-text-box) – Tikkes Oct 04 '16 at 07:55
  • 1
    What re you trying to achive here? this smells of an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – Liam Oct 04 '16 at 07:58
  • ok tnx :) I already found a solution. – Clorge32 Oct 04 '16 at 08:04

2 Answers2

0

Sample Code

HTML:

<input  id="txtCustom" data-defaultText="Hello" />

jQuery:

$("#txtCustom").on('input',function(){
    var self=$(this);
    var changedValue=self.val();
    var defaultValue=self.attr("data-defaultText");

    if(changedValue==defaultValue){
     //  Hide buttob
    }else {
     // show butto=n
    }

});
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
0

this solution works fine for me :)

  $('body').on('change', 'input', function(e){
      var x = $( '#q' ).prop( 'defaultValue' );
      if(x!=$( '#q' ).val() ){
       show button;
      }
    });
Clorge32
  • 341
  • 1
  • 3
  • 7