Mikkel Winther
Mikkel Winther

Reputation: 597

jQuery keydown function not working

I need some help with making the following code work properly:

$('#entry').keydown(function(e) {
    alert(e.keyCode);
});

With this textarea field:

<textarea id="entry" placeholder="Type message here"></textarea>

I have checked with adding alerts outside of the brackets, and the document is loaded and works fine. I even extract a series of entries from a database right after this, with no errors.

What I'm looking to cook here is a textfield that when pressing Enter simply sends the .val() of the field to another function responsible for the AJAX request. But obviously I can't get there when I cannot enter the function.

Upvotes: 0

Views: 483

Answers (2)

ParPar
ParPar

Reputation: 7569

Try:

$('#entry').live('keydown',function(e) {
    alert(e.keyCode);
});

Upvotes: 1

adeneo
adeneo

Reputation: 318302

$(function() {
    $('#entry').on('keyup', function(e) {
        if (e.which == 13) someotherFunction(this.value);
    });
});

FIDDLE

Upvotes: 4

Related Questions