Forum Moderators: open

Message Too Old, No Replies

Pressing the escape key twice clears form fields

Pressing the escape key twice clears form fields

         

Civichunter

12:01 am on Oct 4, 2005 (gmt 0)

10+ Year Member



Hi Guys,

Another problem with forms. When users are filling out form fields and presses the escape key (once or twice) by default most browsers automatically clears the form field of all text. Is there a way to disable this at all?
(I know... why would people press the escape key when filling out a form?.. lets just say that most of my targeted audience is not so bright with web forms)
Any feedback is appreciated.

Thank you

Dijkgraaf

1:09 am on Oct 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well you would have to trap the key strokes.
Below example is IE. It was a function that stopped various other things as well such as F5 which I have removed, hence it is probably more verbose than you need, also it needs some adapting to make it compatible for other browsers.

document.onkeydown = showDown;

function showDown(evt) {
evt = (evt)? evt : ((event)? event : null);
if (evt) {
if (event.keyCode==27 && (event.srcElement.type == "text" ¦¦ event.srcElement.type == "textarea" ¦¦ event.srcElement.type == "password") ) {
// stop escape
cancelKey(evt);
return false;
}
}
}

function cancelKey(evt) {
if (evt.preventDefault) {
evt.preventDefault();
return false;
}
else {
evt.keyCode = 0;
evt.returnValue = false;
}
}

Civichunter

11:22 pm on Oct 6, 2005 (gmt 0)

10+ Year Member



Hi Dijkgraaf

Thanks for the help, I'll try to modify the code to work in FF1 as well.

Cheers,

Simon