09.02.2015 Views

DOM Traversal Methods - MarkMail

DOM Traversal Methods - MarkMail

DOM Traversal Methods - MarkMail

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5<br />

Description<br />

This handler is a shortcut for .bind('change', handler) in the first variation, and<br />

.trigger('change') in the second.<br />

The change event is sent to an element when its value changes. This event is limited<br />

to fields, boxes, and elements. For<br />

select boxes, the event is fired immediately when the user makes a selection with the<br />

mouse, but for the other element types the event is deferred until the element loses<br />

focus.<br />

For example, consider the HTML:<br />

<br />

<br />

<br />

Option 1<br />

Option 2<br />

<br />

<br />

Trigger<br />

The event handler can be bound to the text input and the select box:<br />

$('.target').change(function() {<br />

$(this).log('Change event was triggered.');<br />

});<br />

Now when the second option is selected from the dropdown, the message is<br />

displayed. It is also displayed if we change the text in the field and then click away.<br />

If the field loses focus without the contents having changed, though, the event is not<br />

triggered. We can trigger the event manually when the button is clicked:<br />

$('.trigger').click(function() {<br />

$('.target').change();<br />

});<br />

After this code executes, clicks on the trigger button will also display the message.<br />

The message will be displayed twice, because the handler has been bound to the<br />

change event on both of the form elements.<br />

[ 121 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!