views:

60

answers:

2

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?

+1  A: 

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);
}
Gordon Tucker
Thanks, that helped a lot.
BiscottiGummyBears
+1  A: 

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
});
Anurag