DHTML KeyCode Example
by Jean-Marc Autexier, 07/05/2009

This page shows how one can enter a text directly into a web page, without using an input field.

Enter your text:

How does it work?? Here's the code:

<script language="javascript" type="text/javascript">
// This function will receive key press events from documents
function keypress(e){

// Some cross browser stuff to get the keyCode
if(!e) var e=window.event;
if(e.keyCode) keyCode=e.keyCode; else if(e.which) keyCode=e.which;

// Append new ney character of the key code to the HTML document
document.getElementById("yourtext").innerHTML += String.fromCharCode(keyCode);

return true;
}

// This function register the method keypress() as keydown/press listener
function keypresslistener() {
if(document.all) {
document.onkeydown = keypress;
}
else if( document.layers || document.getElementById) {
document.onkeypress = keypress;
}
}
</script>
</head>
<body onload="keypresslistener();">
This page shows how one can enter a text directlay into a web page, without using an input field.
<br><br>

// This is the element to which we will append the new characters
Enter your text: <span id="yourtext"></span>

Image all the nice things one can do with that? enter text directly in a table .... the text you have entered above is also written into the cell. Now combine this with the selection of the cell with your mouse...

RowLastnameFirstname
1   
2  


Links: