Chris
Chris

Reputation: 1505

Detecting a keypress within a form?

I'm trying to get a web page detect user keypresses within a particular form (there is a function to be called on each keypress). However my code is not working (console never prints to the log):

$(document).on("keypress","filterinput",function(e) {
    console.log("press detected");
});

<form class="filterform"><input class="filterinput" type="text"></form>

Upvotes: 0

Views: 266

Answers (4)

Nick
Nick

Reputation: 4212

Try this code

$(".filterinput").keypress(function(e)
    {
        console.log("press detected");
    });

    <form class="filterform"><input class="filterinput" type="text"></form>

Upvotes: 0

jbabey
jbabey

Reputation: 46647

You're missing the period . in your selector that signifies "look for a class":

$(document).on("keypress",".filterinput",function(e) {
//                         ^ period

Upvotes: 0

Omer Bokhari
Omer Bokhari

Reputation: 59578

your selector should be .filterinput

Upvotes: 0

Joseph Silber
Joseph Silber

Reputation: 219938

You forgot the . in the class selector:

$(document).on("keypress", ".filterinput", function(e) {
//                          ^ this . indicates that it's a class selector
    console.log("press detected");
});

Upvotes: 2

Related Questions