15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

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 13 ✦ Event Objects (Chapter 29)<br />

here is the event that triggers the function to show the properties. From the text<br />

box, an onKeyPress event handler triggers that process; from the button, an<br />

onClick event handler does the job.<br />

NN6+ event Object<br />

charCode<br />

keyCode<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓<br />

Example<br />

Listing 29-18 provides a play area to view the charCode and keyCode properties<br />

for all three keyboard events while you type into a TEXTAREA. You can use this<br />

later as an authoring tool to grab the precise codes for keyboard keys you may not<br />

be familiar with.<br />

Listing 29-18: Displaying charCode and keyCode<br />

Property Values<br />

<br />

<br />

charCode and keyCode Properties<br />

<br />

TD {text-align:center}<br />

<br />

<br />

function showCode(which, evt) {<br />

document.forms[0].elements[which + “Char”].value = evt.charCode<br />

document.forms[0].elements[which + “Key”].value = evt.keyCode<br />

}<br />

function clearEm() {<br />

for (var i = 1; i < document.forms[0].elements.length; i++) {<br />

document.forms[0].elements[i].value = “”<br />

}<br />

}<br />

<br />

<br />

<br />

charCode and keyCode Properties<br />

<br />

<br />

<br />

<br />

Continued<br />

423<br />

(NN6) eventObject.charCode

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

Saved successfully!

Ooh no, something went wrong!