.change()
JQ Home <<
Events <<
.change()
Change event handler.
Description
The .change()
method is used to bind an event handler to The JavaScript change
event or trigger that event on the specified element.
- The
change
event is sent to an element when the element changes. - This event is limited to <input>, <select> and <textarea> elements.
- For select boxes, checkboxes, and radio buttons the event fires as soon as the user makes a selection with the mouse.
- For other input elements and textarea elements the event fires after the user makes a change and loses focus on the element.
Syntax
Signature | Description |
---|---|
.change( ) | Trigger the change JavaScript event on the specified element. |
.change( handler(eventObject) ) | Bind an event handler to the change JavaScript event. |
.change( [eventData ,] handler(eventObject) ) | Bind an event handler to the change JavaScript event, optionally passing an object of data. |
Parameters
Parameter | Description | Type |
---|---|---|
handler( eventObject ) | A function to execute each time the event is triggered. | Function |
eventData | An object of data to pass to the event handler. | Anything |
Return
A jQuery
object.
.change( )
Example
Events << Top
Trigger the change
JavaScript event on the specified element.
- This signature is a shortcut for
.trigger('change')
.
In the example below we show a new message in the 'div' element with an id of 'div1' every time a radio button is changed.
When the radio button is pushed we trigger off the change
JavaScript event on the 'div1'. This then fires off the $('#div1).change(function(){})
code which outputs the message.
$(function(){
$('#div1').change(function () {
$('#div1').append('<code>change</code> JavaScript event triggered.<br>');
});
$('.formfields3 input:radio').click(function() {
$('#div1').change();
});
});
div1. Some initial text.
.change( handler(eventObject) )
Example
Events << Top
Bind an event handler to the change
JavaScript event.
- This signature is a shortcut for
.on('change', handler)
.
In the example below we show a new message in the 'p' element with an id of 'scrollspan1' each time the input element with an id of 'input1' below has some text changed and then loses focus.
When the input is typed in, the change
JavaScript event fires off the addText(event)
mothod which outputs a message.
What we are doing here is passing across the event
object to the function addText(event)
method. The data we specify gets tagged onto the event.data
property.
$(function(){
$('#input1').change(addText);
function addText(event) {
$('#scrollspan1').append('change 1 **JavaScript event triggered**<br>');
}
});
We will show a message here.
.change( [eventData ,] handler(eventObject) )
Example
Events << Top
Bind an event handler to the change
JavaScript event, optionally passing an object of data.
- This signature is a shortcut for
.on('change', handler)
.
In the example below we show a new message in the 'p' element with an id of 'scrollspan2' each time the input element with an id of 'input2' has some text changed and then loses focus.
When the input field gets changed, the change
JavaScript event fires off the $('#input2').change({ param1: '#scrollspan2', param2: 'change 2 ', param3: '**JavaScript event triggered** ' }, addText2);
code.
What we are doing here is passing across the event
object to the function addText2(event)
. The map we specify, in our case { param1: '#scrollspan2', param2: 'change ', param3: '**JavaScript event triggered** ' }
gets tagged onto the event.data
property. We then access this parameter in the function via event.data.param
and use it as part of the appended data.
$(function(){
$('#input2').change({ param1: '#scrollspan2', param2: 'change 2 ',
param3: '**JavaScript event triggered**<br>' }, addText2);
function addText2(event) {
$(event.data.param1).append( event.data.param2 + event.data.param3);
}
});
We will show a message here.
Related Tutorials
jQuery Advanced Tutorials - Lesson 3 - Form Events