The Wayback Machine - https://web.archive.org/web/20240722130456/https://www.geeksforgeeks.org/how-to-detect-if-textbox-content-has-changed-using-javascript/
Open In App

How to detect If textbox content has changed using JavaScript ?

Last Updated : 20 Dec, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

The task is to detect the changes in the textbox content with JavaScript. Whenever a user writes or types something in the input text box we have to detect that event by the use of some methods present in JavaScript.

There are some common approaches that are discussed below:

Approach 1: Using onchange event

We will use the onchange event in the input element and call a function to see the effect.

Example: In this example, we are using the above-explained approach.

html




<!DOCTYPE html>
<html>
 
<head>
  <title>
    Detect If Textbox content has
    changed using pure JavaScript
  </title>
</head>
 
<body style="text-align:center;">
  <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"
         onchange="GFG_Fun()" />
  <br>
  <br>
  <script>
    function GFG_Fun() {
      alert('Changed');
    }
  </script>
</body>
</html>


Output:

Approach 2: Using onpropertychange event

There are few other events that can be used to detect the change in content of textbox. Use any or all of them onchange event, onpropertychange event, onkeyup event, onpaste event and oninput event in the input element and call a function to see the effect.

Example: In this example, we are implementing the above approach.

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Detect If Textbox content has
        changed using pure JavaScript
    </title>
</head>
 
<body style="text-align:center;">
    <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"
        onchange
        onpropertychange
        onkeyuponpaste oninput="GFG_Fun()" />
    <br>
    <br>
    <script>
        function GFG_Fun() {
            alert('Changed');
        }
    </script>
</body>
 
</html>


Output:

Approach 3: Using addEventListener() method

In javascript with the help of the addEventListener() method, we can attach the input javascript event to the textarea.

Example: In this example, we are using the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
  <style>
    h1 {
      text-align: center;
    }
 
    .abc {
      display: flex;
      justify-content: center;
      text-align: center;
    }
  </style>
</head>
 
<body>
  <h1 style="color:green;">
    GeeksforGeeks
  </h1>
  <div class="abc">
    <input id="gfg">
  </div>
 
  <script>
    document.getElementById("gfg")
      .addEventListener("input", (event) => alert("Changed!"));
 
  </script>
</body>
</html>


Output:

Output

 



Similar Reads

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
2 min read
How to detect and change the content/style of a div using jQuery ?
The changes to the html/text of a div can be detected using jQuery on() method. The on() is used to attach one or more event handlers for the selected elements and child elements in the DOM tree. The on() method is the replacement of the bind(), live() and delegate() method from the jQuery version 1.7. Syntax: $(selector).on(event, childSelector, d
2 min read
How to check whether the enter key is pressed in a textbox or not using JavaScript / jQuery ?
Given a textarea element and the task is to check the user presses enter key with the help of JQuery. jQuery keyup() Method: This method triggers the keyup event or adds a function to run when a keyup event occurs. The keyup event occurs when a keyboard key is released. Syntax: Trigger the keyup event for the selected elements: $(selector).keyup()
3 min read
How to trigger HTML button after hitting enter button in textbox using JavaScript ?
In this article, we are given an HTML document containing a text area and the task is to trigger the button when the user hit Enter button. We can do it by using the "keyup", "keydown", or "keypress" event listener on the textbox depending on the need. When this event is triggered, we check if the key pressed is ENTER or not. If the key pressed is
4 min read
How to move button in the same line with Checkbox and Textbox using JavaScript ?
The style "display" property in HTML DOM is used to set elements to the "display" property. To set the button in the same line as other elements of the web page is done using the inline property. Syntax: It returns the "display" property. object.style.display It sets the "display" property. object.style.display = inline; Example: In this example, t
1 min read
How to Display Changed Browser URL Without Reloading Through alert using JavaScript ?
To change the URL in the browser without loading the new page, we can use history.pushState() method and replaceState() method from JavaScript. To display the browser-URL before changing the URL we will use window.location.href in the alert() function and will use again after changing the browsers-URL. Note: The history.pushState() method combines
1 min read
Javascript Program to Minimize characters to be changed to make the left and right rotation of a string same
Given a string S of lowercase English alphabets, the task is to find the minimum number of characters to be changed such that the left and right rotation of the string are the same. Examples: Input: S = “abcd”Output: 2Explanation:String after the left shift: “bcda”String after the right shift: “dabc”Changing the character at position 3 to 'a' and c
3 min read
How to create a simple editable canvas textbox using Fabric.js?
In this article, we are going to create a canvas textbox using Fabric.js. The canvas means text written in the Textbox is movable and can be stretched according to requirement. Further, the text itself can be edited into anything else too because it is a textbox. Creating structure: To make this possible we are going to use a JavaScript library cal
2 min read
How to fill all input fields automatically from database by entering input in one textbox using PHP ?
XAMPP is a free and open-source cross-platform web server solution developed by Apache Friends, consisting mainly of the Apache HTTP Server, MySQL database, and interpreter for scripts written in the PHP programming language. The XAMPP server helps to start Apache and MySQL and connect them with the PHP file. Approach: We need to create a MySQL dat
4 min read
How to allow only 10 numbers in a textbox using jQuery ?
In this article, we will learn about how to allow only 10 numbers in a textbox using jQuery, jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, Given an HTML document containing a textbox input element, the task is to allow the input of strictly 10 numeric digits or numbers in that particular
4 min read
three90RightbarBannerImg