Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery not detecting enter key pressed in textarea

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.

like image 805
Bob Avatar asked Feb 23 '23 03:02

Bob


2 Answers

$('.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

like image 102
Ahmad Alfy Avatar answered Feb 25 '23 17:02

Ahmad Alfy


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
 }
like image 40
mnsr Avatar answered Feb 25 '23 17:02

mnsr