spacer
Yehuda Shiran October 4, 2001
Return-Key-Sensitive Event Handlers
Tips: October 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Mozilla's Ubquity Mashup: For The Masses?
iPhone Users Just Want to Have Fun
Oops! I Fixed the Linux Kernel
When you ask the user to enter a number in a text field, you may want to act on the new entry only after the user hits the RETURN key. You accomplish this by using the all-key onkeyup event handler, and then you use JavaScript to dismiss all events that are triggered by other keys. Play around with the following example. Notice that the zoom factor is updated with every key:

%

Have you read our columns about Print Templates?

Now, repeat with the following text field. Notice that the zoom factor is being updated only when you hit the RETURN key:

%

Have you read our columns about Print Templates?

Here is the code for the RETURN-sensitive case:

<INPUT ID="zoomfactor2" TYPE="text" VALUE="50" SIZE="3" MAXLENGTH="4" onkeyup="updateZoom2()">%
<DIV ID="container2">
Have you read our columns about Print Templates?
</DIV></P>
<SCRIPT LANGUAGE="JavaScript">
<!--
function updateZoom2() {
  if (event.keyCode != 13) return;
  container2.style.zoom = zoomfactor2.value + "%";
}
// -->
</SCRIPT>
The line:

if (event.keyCode != 13) return;
makes sure that only RETURN-key-releasing events are being handled. The key code of the RETURN key is 13.

This tip works on IE 6.0 and above.


People who read this tip also read these tips:

Look for similar tips by subject:



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Performance Optimizations for High Speed JavaScript · Advanced Web Performance Optimization · Simple Comments Meets OpenID
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Extending Telephony: VoIP Call Recording for Business · U-Verse for Business Has Wi-Fi Perks · Lian-Li Launches New Power Supply Line, Rack Mount Kit and Fan Blower