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?
views:
60answers:
2
+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
2010-01-20 00:13:58
Thanks, that helped a lot.
BiscottiGummyBears
2010-01-20 00:45:17
+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
2010-01-20 00:43:12