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 />

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

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

});<br />

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

If key presses anywhere need to be caught (for example, to implement global<br />

shortcut keys on a page), it is useful to attach this behavior to the document object.<br />

All key presses will make their way up the <strong>DOM</strong> to the document object unless<br />

explicitly stopped because of event bubbling.<br />

To determine which key was pressed, we can examine the event object that is passed<br />

to the handler function. The .keyCode attribute typically holds this information, but<br />

in some older browsers .which stores the key code. JavaScript's String object has a<br />

.fromCharCode() method that can be used to convert this numeric code into a string<br />

containing the character for further processing.<br />

Note that keydown and keyup provide a code indicating which key is pressed, while<br />

keypress indicates which character was entered. For example, a lowercase "a" will<br />

be reported as 65 by keydown and keyup, but as 97 by keypress. An uppercase "A"<br />

is reported as 97 by all events. This can be the primary motivator for deciding which<br />

event type to use.<br />

.keyup()<br />

Binds an event handler to the keyup JavaScript event, or triggers that event on<br />

an element.<br />

.keyup(handler)<br />

.keyup()<br />

Parameters (First Version)<br />

• handler: A function to execute each time the event is triggered<br />

Return Value<br />

The jQuery object, for chaining purposes.<br />

Description<br />

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

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

[ 127 ]

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

Saved successfully!

Ooh no, something went wrong!