18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

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.

<strong>For</strong>ms and Data Integrity<br />

<br />

<br />

Select Text Example<br />

<br />

function selectText() {<br />

var oTextbox1 = document.getElementById(“txt1”);<br />

oTextbox1.focus();<br />

oTextbox1.select();<br />

}<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

This example displays a text box and a button. When you click the button, the text in the text box is<br />

selected.<br />

Text box events<br />

Both text box types support the previously mentioned form-field blur and focus events along with two<br />

others: change and select.<br />

❑<br />

❑<br />

change — Occurs when the text box loses focus after the user changed the value (does not fire<br />

if you change the value setting the value property).<br />

select — Occurs when one or more characters have been selected, either manually or by<br />

using the select() method.<br />

Note the difference between the change event and the blur event. The blur event fires whenever the text<br />

box loses focus; the change event fires when the text box loses focus as well, but only if the text in the text<br />

box has changed. If the text is the same and the text box loses focus, only the blur event is fired; if the text<br />

has changed, the change event fires first, followed by the blur event. Try it out to better get the hang of it:<br />

<br />

The select event, on the other hand, has nothing to do with the focus of the text box. This event fires<br />

when one or more characters are selected by the user or when the select() method is called. You can<br />

experiment with it:<br />

<br />

Select text automatically<br />

When a user is entering information into a traditional desktop application, it is not uncommon for the<br />

entire contents of a text box to be highlighted when the user tabs into it. This can be accomplished on<br />

both an input-style text box and a textarea in HTML very easily: Just add the code “this.select()” to<br />

the control’s onfocus event handler:<br />

345

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

Saved successfully!

Ooh no, something went wrong!