What we cannot speak about we must pass over in silenceLudwig Wittgenstein
Relates to Accessibility and DOM Scripting, Firefox and Co
In a posting last month I presented an onkeypress event handling script to avoid the Mozilla browsers triggering the event when the user tabs of the anchor. This was achieved by
calling an intermediary function when the onkeypress event handler was triggered that would call the final function (in this case a style switcher) only if the pressed key was return.
While this resolved the problem, I discovered the other day that a better solution is to just call the onclick event of the same anchor element when the onkeypress event is triggered.
<a href="#" onclick="setStyleSheet('sheet'); return false;"
onkeypress="this.onclick">Switch to List View</a>
This works fine on the Mozilla browsers and does not raise the problem when the function is called directly by the event handler. Also, there is no need to hard code this and it can be extracted to a DOM creation scripting once the page has loaded, thus making the markup ever so slightly purer.
Posted on Wednesday, Oct 15, 2003 at 04:49:05.
Comments on Key Press Event Revisited (3)
α comment
this works nice. I need some help on keypress. onkeypress event for html table works well in IE but the event is not fired in firefox. Have you come across this problem and solved it, if so help me.
Posted by SenthilKumar
Wednesday, Aug 17, 2005 at 10:50:33
β comment
It doesn't fire up in FF because it was never supposed to, Senthil. If you develop something like that now it will not work in a few years when IE follows the standards. Instead you should use the onfocus event handler and DOM.
Posted by Luis
Thursday, Sep 15, 2005 at 13:20:45
γ comment
i am using onkeypress event in IE which is working fine but i what tat if esc key pressed then only particular event should occur.which fails in firefox.pls help me out.
function pressEsc() { alert(window.event.keyCode); if(window.event.keyCode==27) { var div = document.getElementById('2nd'); div.style.display='none'; return false; } }
Posted by abhishake
Tuesday, Mar 28, 2006 at 07:11:48