How to call a function when content of a div changes using jQuery ?
The task is to call a function when the content of a div is changed. You can achieve this task by using the in-built method known as .change(). In simple words whenever the field is been changed we call a function that will throw a pop-up.
.change(): This method will only trigger when some change occurs in the element or field. It accepts an optional function as an argument that will perform some kind of work.
selector: It will select an element on which we have to perform this task.
function(): It is an optional argument that will specify the function to run when the change event occurs for the selected elements.
Approach 1: In this approach, we are going to implement its execution without taking any parameter such as function.
- First, create an input box with some value and onChange attribute.
- Create a button and map it to the script where you have written the change method without any argument.
Explanation: When you click a button trigger change event of the input field occurs and pop-up window will appear and display whatever present in the input field.
Approach 2: In this approach, we are going to implement its execution with the help of function as an argument in the change method.
- Make an input field of text type.
- Map input element to the change() method and write some text to show on function call.
Explanation: When you enter something in the input field and press enter then the change event will run and executes the function and show the message that is written in the function.