Search code examples
javascriptprototypejs

Using Prototype- how do I watch input field for carriage return?


I'm using the JS library Prototype-- how can I watch a text input field and when return is pressed, alert the contents of the field to the user? (There is no < form > on the page, just the input field.)

Here's roughly what I want to do:

Enter something: <input type='text' id='myfield'>

<script>
  if([keydown event in #myfield]) {
     alert("You typed: "+[contents of #myfield]);
  }
</script>

Thanks.


Solution

  • $('myfield').observe('keypress', function(event){
        if(event.keyCode == Event.KEY_RETURN)
            alert($('myfield').value);
    });​
    

    You can find a simple demo in this jsfiddle