Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect a textbox's content has changed

I want to detect whenever a textbox's content has changed. I can use the keyup method, but that will also detect keystrokes which do not generate letters, like the arrow keys. I thought of two methods of doing this using the keyup event:

  1. Check explictly if the ascii code of the pressed key is a letter\backspace\delete
  2. Use closures to remember what was the text in the textbox before the key stroke and check whether this has changed.

Both look kinda cumbersome.

like image 785
olamundo Avatar asked Sep 26 '09 12:09

olamundo


People also ask

How to detect change in textbox?

Answer: Use the input Event You can bind the input event to an input text box using on() method to detect any change in it. The following example will display the entered value when you type something inside the input field.

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.

Is jQuery a change?

The change() is an inbuilt method in jQuery that is used to detect the change in value of input fields. This method works only on the “<input>, <textarea> and <select>” elements. Parameter: It accepts an optional parameter “function”. Return Value: It returns the element with the modification.


1 Answers

Start observing 'input' event instead of 'change'.

jQuery('#some_text_box').on('input', function() {     // do your stuff }); 

...which is nice and clean, but may be extended further to:

jQuery('#some_text_box').on('input propertychange paste', function() {     // do your stuff }); 
like image 98
Atul Vani Avatar answered Oct 07 '22 08:10

Atul Vani