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.

362<br />

<strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

Listing 25-2 (continued)<br />

<br />

<br />

Enter lowercase letters for conversion to uppercase: <br />

<br />

<br />

<br />

I also show two other ways to accomplish the same task, each one more efficient<br />

than the previous example. Both utilize the shortcut object reference to get at the<br />

heart of the text object. Listing 25-3 passes the text object — contained in the this<br />

reference — to the function handler. Because that text object contains a complete<br />

reference to it (out of sight, but there just the same), you can access the value<br />

property of that object and assign a string to that object’s value property in a simple<br />

assignment statement.<br />

Listing 25-3: Passing a Text Object (as this) to the Function<br />

<br />

<br />

Text Object Value<br />

<br />

function upperMe(field) {<br />

field.value = field.value.toUpperCase()<br />

}<br />

<br />

<br />

<br />

<br />

Enter lowercase letters for conversion to uppercase: <br />

<br />

<br />

<br />

Yet another way is to deal with the field values directly in an embedded event<br />

handler — instead of calling an external function (which is easier to maintain<br />

because all scripts are grouped together in the Head). With the function removed<br />

from the document, the event handler attribute of the tag changes to do<br />

all the work:<br />

<br />

document.formObject.textObject.value

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

Saved successfully!

Ooh no, something went wrong!