/ Published in: JavaScript
I've found a very nice (and standard) solution to firing events while it's not normally available, this piece of code searchs the active element (a text input) inside the Event.observers collection and fires the "onchange" event (normally not possible in explorer) when the return key is pressed.
- this code requires prototype.js -
Hope you enjoy it!
Expand |
Embed | Plain Text
$A(document.getElementsByClassName('text', $('main-form'))).each(function(poElement){ Event.observe(poElement, 'change', function(e){ // this event only fires while tabbing after changing content Element.addClassName(poElement, 'changed'); }, false); Event.observe(poElement, 'keypress', function(e){ var cKeyCode = e.keyCode || e.which; if (cKeyCode == Event.KEY_RETURN){ // but now will fire also while pressing return key Event.observers.findAll(function(poEventObserver){ return poEventObserver[0] == poElement && poEventObserver[1] == "change"; })[0][2](); } }, false); });
Comments
Subscribe to comments
You need to login to post a comment.

Excellent example - thanks... what I needed was to see the syntax for the keypress event trigger on a specific object... simplified to:
Event.observe($("objid"), "keypress", function(e){ var cKeyCode = e.keyCode || e.which; if (cKeyCode == Event.KEY_RETURN){ alert("do something"); } }, false);
thanks for the example...