Open In App

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:

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:



Example-2: In this example, alert box appears saying that ‘Text content changed!’ when either of activity happens.




<!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:


Article Tags :