BiscottiGummyBears
BiscottiGummyBears

Reputation: 365

Mootools events question

I have the same code for keyup and keydown events, is there any way that I can pass the element to the function so I can call it like element.addEvent('keyup',function(event,element)); on both instead of having to have an inline function?

Upvotes: 1

Views: 152

Answers (2)

Anurag
Anurag

Reputation: 141919

Inside the event callback, this refers to the element that fired the event. So you can write either:

function valueChangeHandler() {
    console.log(this.value);
}

or instead, use the target property of the event object. Both work the same.

function valueChangeHandler(event) {
    console.log(event.target.value);
}

Attach the callback function using either addEvent or addEvents:

$("someInputId").addEvents({
    "keyup": valueChangeHandler,
    "keydown": valueChangeHandler
});

Upvotes: 1

Gordon Tucker
Gordon Tucker

Reputation: 6774

You can use event.target to get the element I believe

window.addEvent('domready', function(){
    $('yourInputField').addEvents({
       'keyup': alertValue,
       'keydown': alertValue
    });
});

function alertValue(event){
    alert(event.target.value);
}

Upvotes: 2

Related Questions