Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery catch a first keypress only?

Tags:

jquery

I have this code:

 $j("#regfname").keypress(function () {
    alert('Handler for .keypress() called.');
});

and want to execute only once... or only for the first keypress ..what's the most optimal way to do it?

like image 586
Stewie Griffin Avatar asked Feb 03 '11 21:02

Stewie Griffin


2 Answers

You can use the jQuery one() method to only execute the event once per element.

$j("#regfname").one("keypress", function () {
    alert('Handler for .keypress() called.');
});
like image 54
Brandon Avatar answered Oct 01 '22 15:10

Brandon


You want to use the one function:

$j('#regfname').one('keypress', function(){...});
like image 45
zzzzBov Avatar answered Oct 01 '22 16:10

zzzzBov