[TriLUG] javascript backspace || edit in textbox

Joseph Mack NA3T jmack at wm7d.net
Fri Aug 21 15:46:08 EDT 2009

What I would like is for the user to click on the text box. 
If they then backspace, the text box clears. Otherwise any 
other keystrokes are allowed to do normal editing (including 
a subsequent use of the backspace).

Here's the javascript so far. It just clears the textbox on 
a click.

<script language="JavaScript" type="text/JavaScript">
function clearbox(thebox){
         thebox.value = "";

Here I simulate the user's previous search with defaults.

<INPUT type="text"  name="/author" size="30" value="Mark Twain" maxlength="100" onClick="clearbox(this);">

As a first step, clearing the box is fine. I now want to fix 
the function so that it watchs for keystrokes. If the first 
keystroke is a backspace, test with

if (evt.keyCode == "8" )

and allow the line


to run, otherwise the keystroke is left in whatever buffer 
it sits in, and the function just exits. As long as the user 
doesn't click again (to avoid invoking the function) all 
subsequent keystrokes (I hope) will just be editing.

My problem is that I don't know how to get the function to 
read a single keystroke. I only know how to read keystrokes 
with lines in the html like

onKeyPress = myFunction(event);

Any ideas?

Thanks Joe

Joseph Mack NA3T EME(B,D), FM05lw North Carolina
jmack (at) wm7d (dot) net - azimuthal equidistant map
generator at http://www.wm7d.net/azproj.shtml
Homepage http://www.austintek.com/ It's GNU/Linux!

More information about the TriLUG mailing list