Prototype events: use the input value since the event was generated

I recently read how to do this, but can't find it again!

In fact, I create some events that listen on the input data of the form and update their values ​​when triggered. How do I pass the initial input value (input value when creating the event) to the event so that I can compare it with the current value?

+4
source share
2 answers

Just store it in a variable outside the event processing closure, for example:

var original = $('someinput').getValue(); $('someinput').observe('whatever_event', function(ev) { $('someinput').getValue(); // contains current value original; // contains original value }); 

UPDATE. Replace the above code with the prototype. The jQuery version is below for reference.

 var original = $('#someinput').val(); $('#someinput').whatever_event(function(ev) { this.val(); // contains current value original; // contains original value }); 
+3
source

The DOM already saves this value for you (for input types, not select ).

 element.observe('click', function(){ // In this context, this == element if (this.defaultValue != this.value) { // value is different } } 
0
source

Source: https://habr.com/ru/post/1336045/


All Articles