Ankit Ladhania
Ankit Ladhania

Reputation: 995

how to find two keydown event has fired in jquery

how to find if two keydown event has fired. I'm creating a search box in which after typing two letters the results slides in the page.. any suggestion

Upvotes: 0

Views: 91

Answers (3)

Ed W
Ed W

Reputation: 125

Using .keydown() you can check the sender input length (what's in the search box) is greater than 2.

http://api.jquery.com/keydown/

For example

$('#myTextBox').keydown(function() {
    alert($(this).val().length);
})

However, it sounds like you might benefit from using jQueryUI and Autocomplete. http://jqueryui.com/autocomplete/

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

you could just count the characters, like

$("#your_input_id").on("keyup", function() {
   if( $.trim($(this).val().length) >= 2 ) {
       //show suggestions
   }
});

Upvotes: 1

Amar Banerjee
Amar Banerjee

Reputation: 5012

You have to call a function in keyup event and then count the length of input of that input box.

Example:

<input name="test" id="test" onkeyup="search();"/>

<script>
function search()
{
  var textBox = document.getElementById("test");
  var textLength = textBox.value.length;
  if(textLength > 1)
  {
      // Do your stuff 
  }
}

</script>

Use it wisely it should work. Cheers :)

Upvotes: 1

Related Questions