Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html catch event when user is typing into a text input

Im just wondering how I go about catching the event when the user is typing into a text input field on my web application.

Scenario is, I have a contacts listing grid. At the top of the form the user can type the name of the contact they are trying to find. Once there is more than 1 character in the text input I want to start searching for contacts in the system which contain those characters entered by the user. As they keep typing the data changes.

All it is really is a simple type ahead type functionality (or autocomplete) but I want to fire off data in a different control.

I can get the text out of the input once the input has lost focus fine, but this doesnt fit the situation.

Any ideas?

Thanks

like image 902
Matt Avatar asked Dec 11 '12 21:12

Matt


People also ask

What event should you listen to to catch when a user enters a form field?

The oninput event occurs when an element gets user input.

How will you get text inside an input tag event?

Answer: Use the value Property You can simply use the value property of the DOM input element to get the value of text input field. The following example will display the entered text in the input field on button click using JavaScript.


3 Answers

Use the keyup event to capture the value as the user types, and do whatever it is you do to search for that value :

$('input').on('keyup', function() {
     if (this.value.length > 1) {
          // do search for this.value here
     }
});

Another option would be the input event, that catches any input, from keys, pasting etc.

like image 59
adeneo Avatar answered Oct 17 '22 00:10

adeneo


Why not use the HTML oninput event?

<input type="text" oninput="searchContacts()">
like image 24
Hiram A Avatar answered Oct 16 '22 23:10

Hiram A


I would use the 'input' and 'propertychange' events. They fire on cut and paste via the mouse as well.

Also, consider debouncing your event handler so that fast typists are not penalized by many DOM refreshes.

like image 28
syazdani Avatar answered Oct 17 '22 01:10

syazdani