.change() methodThe .change() method in jQuery is used to bind a change event handler to that event on the matched elements.
It also used to trigger change event on the matched elements.
.change() method Syntax.change() .change(handler) .change(eventData,handler)
| Parameter | Type | Description |
|---|---|---|
handler |
Function | A function to execute when the change is triggered on the matched element. |
eventData |
Anything | Additional data passed to the event handler. |
.change() method ExampleTrigger the change event on input #username:
$("#username").change();
Attaches a change event to the input #username that get it's value and validate it.
HTML
<form>
<input type="text" id="username" value="username">
<input type="text" id="email" value="email">
</form>
<button>Validate</button>
$( "#username" ).change(function() {
var testValue = $(this).val(),
expr = /^[A-za-z]{3,16}$/;
if(expr.test(inputValue)){
// success
alert('Validtate the username successfully!')
}
else{
alert('The username is only character allowed!')
}
});
$('button').click(function(){
$( "#username" ).change();
});
