change() method


change() method

The change event occurs when the value of an element has been changed (only works on <input>, <textarea> and <select> elements).The change() method triggers the change event, or attaches a function to run when a change event occurs. This event is limited to <input> elements, <textarea> boxes and <select> elements.


Syntex:

$(selector).change()

Example :-


<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("input").change(function(){
        alert("change event fired because The text of input field has been changed.");
    });
});
</script>
</head>
<body>

<input type="text">

<p>Write something in the input field, and then press enter or Tab or click outside the input field.</p>

</body>
</html>