jQuery Tutorial Tutorials - jQuery .change() method

jQuery .change() method

The .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.

jQuery .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.

jQuery .change() method Example

example

Trigger the change event on input #username:

$("#username").change();

example

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();
});

Try now

Date:2019-08-29 19:07:22 From:www.Lautturi.com author:Lautturi