views:

60

answers:

4
   <input type="text" id="search" size="25" autocomplete="off"/>

I know it is something with

onkeydown="if (event.keyCode == 27)
+1  A: 
function keyPressed(evt) {
 if (evt.keyCode == 27) {
    //clear your textbox content here...
    document.getElementById("search").value = '';
 }
}

Then in your input tag...

<input type="text" onkeypress="keyPressed(event)" id="search" ...>
mamoo
Doesn't work on Chrome/Safari.
Darin Dimitrov
Thank you for your help Mamoo!
jprim
@Darin: thanks for pointing this out :)
mamoo
+2  A: 

Declare a function which will be called when a key is pressed:

function onkeypressed(evt, input) {
    var code = evt.charCode || evt.keyCode;
    if (code == 27) {
        input.value = '';
    }
}

And the corresponding markup:

<input type="text" id="search" size="25" autocomplete="off" 
       onkeydown="onkeypressed(event, this);" />
Darin Dimitrov
That works perfect, thank you very much for your help Darin - I really appreciate it :)
jprim
Darin - if you would also help me out with this one:http://stackoverflow.com/questions/3615351/styling-highlighted-text-in-incremental-search-jsThat would be great!
jprim
Darin: for the keydown event, it's always `keyCode`. All the major browsers have `keyCode` so there's no need to check `charCode`, which is zero or undefined in all current browsers anyway.
Tim Down
+1  A: 
<html><head>
<script>
<!--
function keycheck(event)
{
if(event.keyCode==27){
alert("ESC key is pressed");
document.getElementById("keytxt").value = '';
}
}
-->
</script>
</head>

<body >
Escape key tracking...
<input type="text" id="keytxt" onkeydown="return keycheck(event);" />
</body>
</html>
org.life.java
Doesn't work on Chrome/Safari.
Darin Dimitrov
Edited and tested in chrome / FF. Thanks for pointing it.
org.life.java
Thank you for your help org.life.java!
jprim
A: 
<input type="text" value="" onkeyup="if ( event.keyCode == 27 ) this.value=''" />

This should work.

heb
Thanks for your help heb!
jprim