JQuery | Detect a textbox content is changed or not
In order to detect the text content of input is changed or not, We are using the .on() method of JQuery.
.on()
This is a built-in method provided by jQuery and is used to attach event handlers for the selected elements and their child elements.
Syntax:
$(selector).on(event, childSel, data, fun, map)
parameters:
- event: This parameter is required. It defines events or namespaces to attach to elements selected. If multiple events are to be provided, must be separated by space.
- childSel: This parameter is optional. It defines that the event handler should only be attached to the defined child elements.
- data: This parameter is optional. It specifies the additional data to pass to the function.
- fun: This parameter is optional. It specifies the function to run on event occur.
- map: This parameter specifies an event map ({event: function, event: function, …}) containing one or more event and functions to run when the events occur
Example-1: In this example, alert box appears saying that ‘Text content changed!’ when the text of the input changed.
<!DOCTYPE html> <html> <head> <title> JQuery | detect a textbox's content has changed. </title> <script src= </script> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p> Change the text of input text and click outside of it to see. </p> <input id="input" name="input"/> <br> <br> <script> $("#input").on("change", function() { alert('Text content changed!'); }); </script> </body> </html> |
Output:

-
After clicking outside of the input box:
Example-2: In this example, alert box appears saying that ‘Text content changed!’ when either of activity happens.
- text of the input changed.
- keyup event happens.
- Something is pasted to the input box.
- propertychange occurs.
<!DOCTYPE html> <html> <head> <title> JQuery | detect a textbox's content has changed. </title> <script src= </script> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p> Change the text of input text and click outside of it to see. </p> <input id="input" name="input" /> <br> <br> <script> $("#input").on( "propertychange change keyup paste input", function() { alert('Text content changed!'); }); </script> </body> </html> |
Output:

-
After event occurs:
Recommended Posts:
- How to detect and change the content/style of a div using jQuery ?
- How to check whether the enter key is pressed in a textbox or not using JavaScript / jQuery ?
- How to detect a mobile device in jQuery?
- How to detect escape key press using jQuery?
- How to detect the user's device using jQuery ?
- How to detect value change on hidden input field in JQuery ?
- jQuery | Get Content and Attributes
- jQuery | Set Content and Attributes
- How to copy the content of a div into another div using jQuery ?
- How to insert HTML content into an iFrame using jQuery?
- How to check a selector matches some content using jQuery?
- Content editable change event in jQuery
- Detect a device is iOS or not using JavaScript
- How to detect the change in DIV's dimension ?
- jQuery | jQuery.fx.interval Property with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



