Wednesday, March 21, 2012

Trouble with $addHandler and return values.

I'm using $addHandler to wire up a keydown event to a TextBox control, like such:

function pageLoad() {
$addHandler($get('TextBox1'), "keydown", TextBox1_KeyDown);
}
function TextBox1_KeyDown(eventArgs) {
if (eventArgs.keyCode == 13)
return false;
}

If I explicitly specify OnKeyDown attribute of the TextBox, this works. However, when I add it using $addHandler, it acts as though the return value isn't being passed back through. I set a breakpoint on the conditional and verified that it is correctly returning false, but the browser submits anyway.

Is there any way to make a "KeyDown" handler using $addHandler that'll pass the function's return value through to the element?

Hi,

did you try with evt.preventDefault() ?


I wasn't familiar with that method. I just tried it like this:

function pageLoad() {
var evt = new Sys.UI.DomEvent($get('TextBox1'));
evt.preventDefault();
}
It didn't seem to have any effect either way. Am I doing that wrong?

Hi,

sorry, I don't understand what you're trying to do in the previous snippet. I was referring to the first snippet you posted, which could be modified as follows:

$addHandler($get('TextBox1'),'keydown', function(evt) {
if(evt.keyCode === 13) {
evt.preventDefault();
}
});
The preventDefault method should prevent the default action for a DOM event from being performed.
That's perfect. Thanks!

No comments:

Post a Comment