views:

72

answers:

5

Hi, I'm pretty new to javascript, so bear with me if this is obvious.

Basically what I would like is to implement a little interface where when you type something in a textbox it gives an event. Sorta like how this stack overflow post preview works. I tried hooking into the OnChange event, but that only gives an event when they are done editing the field(and the textbox loses focus). What I want is each time they do anything to the text inside the textbox to get an event..

+2  A: 

Oh, try the onkeydown event.

Here's a dumb example:

<html>
<body>
<form action="" method="POST" id="myForm">
<input type="text" name="myText" onkeydown="doAlert(event);"/>

<script type="text/javascript" language="JavaScript">

function doAlert(e) {

  if(window.event)
  {
      alert( window.event.keyCode);
  }
  else if(e)
  {
      return alert(e.which);
  }
  else
  {
      return null;
  }

}

</script>
</form>
</body>
</html>
BobbyShaftoe
A: 

onkeypress might be what you want.

Lance Rushing
+5  A: 

You want the keypress events described here.

Steerpike
Nice. Even has browser compatibility charts. This has my vote for thoroughness.
John K
A: 

try onkeydown, onkeypress or onkeyup

danimajo
+1  A: 

You may try the onkeyup event:

<html>
<head>
    <title>Test</title>
</head>
<body>

<input type="text" name="test" value="" onkeyup="javascript:document.getElementById('preview').innerHTML = this.value;" />

<div id="preview"></div>

</body>
</html>
Darin Dimitrov