aceenders
aceenders

Reputation: 118

Prototype Events: Use Input Value From When Event is Created

I have recently read how to do this but cannot for the life of me find it again!

Basically, I'm creating some events that listen to form inputs and update their values when triggered. How do I pass the initial input value (the value of the input when the event is created) to the event so that I can compare it with the current value?

Upvotes: 4

Views: 621

Answers (2)

Ben Lee
Ben Lee

Reputation: 53329

Just store it in a variable outside the event handling closure, like this:

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

UPDATE: Replaced the above code with Prototype. jQuery version below for reference.

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

Upvotes: 3

clockworkgeek
clockworkgeek

Reputation: 37700

The DOM already stores that 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
    }
}

Upvotes: 0

Related Questions