Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

action keyup after the third charecter

Hi could someone help me figure out how to stop a function running until a specific number of characters are pressed?

currently using the following function:

$('input#q').keyup

this works as soon as you press any key...

like image 434
Andy Avatar asked Dec 22 '10 16:12

Andy


People also ask

Why is Keyup not working?

keyup / keydown seem to only work on elements that are present at document. ready . If you are triggering your event from elements that were altered or injected post pageload, these events will not fire.

What does Keyup return?

The keyup event is fired when a key is released. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered. For example, a lowercase "a" will be reported as 65 by keydown and keyup , but as 97 by keypress .

How do I get rid of Keyup event?

How to remove keyup event in jQuery? jQuery unbind() Method The unbind() method was deprecated in version 3.0. Use the off() method instead. The unbind() method removes event handlers from selected elements.


2 Answers

Something like this should start firing code after 3 letters have been added:

Live Example

JavaScript

$('input#q').keyup( function() {
   if( this.value.length < 4 ) return;
   /* code to run below */
   $('#output').val(this.value); 
});

HTML

<input id="q" />
<br /><br />
<input id="output"/>
like image 191
subhaze Avatar answered Sep 27 '22 00:09

subhaze


you could do :

$('input#q').keyup(function(){
  if($(this).val().length > 3)
  {
    //do something
  }
});
like image 37
picus Avatar answered Sep 23 '22 00:09

picus