4

My setup: jQuery 1.6.2

I have this HTML

<textarea class="comment_box"> Write a comment...</textarea>  

And the following Javascript

<script>
$('.comment_box').keydown(function (e){
    if(e.keyCode == 13){
        alert('you pressed enter ^_^');
    }
})
</script>

When I press the enter key in the textarea, nothing triggers

EDIT Oops, cut and paste error, I do have $ in my code and it still doesn't work, must be something else going on.

My bad, it is a user operator error, it does work. Sorry for the confusion.

Bob
  • 8,424
  • 17
  • 72
  • 110
  • Guess is a type when you past code here. But `('.comment_box')` shouldn't be `$('.comment_box')`? – Prusse Aug 29 '11 at 23:42

3 Answers3

6
$('.comment_box').keypress(function(event) {
    // Check the keyCode and if the user pressed Enter (code = 13) 
    if (event.keyCode == 13) {
        alert('you pressed enter ^_^');
    }
});

Thats it

Ahmad Alfy
  • 13,107
  • 6
  • 65
  • 99
2

Check out this answer:

jQuery Event Keypress: Which key was pressed?

var code = (e.keyCode ? e.keyCode : e.which);
 if(code == 13) { //Enter keycode
   //Do something
 }
Community
  • 1
  • 1
mnsr
  • 12,337
  • 4
  • 53
  • 79
0

For jQuery you need to use the $ to specify.

$('.comment_box').keyd

should do it.

griegs
  • 22,624
  • 33
  • 128
  • 205