Even if you keyup keydown / keyup , these are the only events that the tab key triggers, you still need some way to prevent the default action, moving to the next element in tab order.
In Firefox, you can call the preventDefault() method on an event object that is passed to your event handler. In IE, you should return false from the event descriptor. The jQuery library provides a preventDefault method for its event object, which works in IE and FF.
<body> <input type="text" id="myInput"> <script type="text/javascript"> var myInput = document.getElementById("myInput"); if(myInput.addEventListener ) { myInput.addEventListener('keydown',this.keyHandler,false); } else if(myInput.attachEvent ) { myInput.attachEvent('onkeydown',this.keyHandler); } function keyHandler(e) { var TABKEY = 9; if(e.keyCode == TABKEY) { this.value += " "; if(e.preventDefault) { e.preventDefault(); } return false; } } </script> </body>
ScottKoon Aug 16 '08 at 13:55 2008-08-16 13:55
source share