JavaScript
  Home arrow JavaScript arrow Page 7 - Understanding The JavaScript Event Mod...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Understanding The JavaScript Event Model (part 1)
By: Team Melonfire, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 35
    2002-06-25

    Table of Contents:
  • Understanding The JavaScript Event Model (part 1)
  • Popeye() And Olive()
  • Handling Things
  • Red Alert
  • Mouse Hunt
  • Forty Two
  • Flavour Of The Month
  • Linking Up
  • Game Over

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Dell PowerEdge Servers

    Understanding The JavaScript Event Model (part 1) - Flavour Of The Month
    (Page 7 of 9 )

    You can also use the onFocus and onBlur handlers to perform actions when the user tabs into or out of a form field. Here's a quick example:

    <html>
    <head>
    </head>
    <body>
    <form action="submit.php" method="post">
    <input type="text" name="box" value="Click me" size="20"
    onFocus="document.forms[0].box.value='Now click outside'"
    onBlur="document.forms[0].box.value='Click me'"> </form>
    </body>
    </html>
    In this case, every time the user clicks inside or outside the text field, either the onFocus() or onBlur() handler is triggered and an appropriate message displayed.

    The next example reworks the one on the previous page to perform field validation when the user tabs out of each field on a form:
    <html>
    <head>
    <script language="JavaScript">
    // check name field
    function checkName()
    {
    // if empty, pop up alert
    if (document.forms[0].name.value == "")
    {
    alert("Please enter a valid name");
    return false;
    }
    else
    {
    return true;
    }
    }
    // check email field
    function checkEmail()
    {
    var flag;
    var str = document.forms[0].email.value;
    // regex to match email addresses
    var pattern =
    /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/;
    var flag = pattern.test(str);
    if(!flag)
    {
    alert ("Please enter a valid email address");
    return false;
    }
    else
    {
    return true;
    }
    }
    </script>
    </head>
    <body>
    <form action="submit.php" method="post">
    Please enter your name.
    <br>
    <input type="text" name="name" onBlur="checkName()">
    <p>
    Please enter your email address.
    <br>
    <input type="text" name="email" size="25" onBlur="checkEmail()"> <br>
    <input type="submit" value="Hit me!"> </form>
    </body>
    </html>
    Another very useful event handler, especially in the context of forms, is one you've already seen before - the onClick handler. Consider the following example, which demonstrates it in the context of a radio button,
    <html>
    <head>
    <script language="JavaScript">
    function displayFlavour()
    {
    for(i=0;i<document.displayForm.flavourSelect.length;i++)
    {
    if(document.displayForm.flavourSelect[i].checked)
    {
    alert("You picked " +
    document.displayForm.flavourSelect[i].value);
    }
    }
    }
    </script>
    </head>
    <body>
    <form name="displayForm" action="submit.php" method="post"> Pick a
    flavour: <br> <input type="radio" name="flavourSelect"
    value="Strawberry" onClick="displayFlavour()">Strawberry
    <br>
    <input type="radio" name="flavourSelect" value="Raspberry"
    onClick="displayFlavour()">Raspberry
    <br>
    <input type="radio" name="flavourSelect" value="Chocolate"
    onClick="displayFlavour()">Chocolate
    </form>
    </body>
    </html>
    and this one, which demonstrates it in the context of a check box.
    <html>
    <head>
    <script language="JavaScript">
    function displayFlavour()
    {
    for(i=0;i<document.displayForm.flavourSelect.length;i++)
    {
    if(document.displayForm.flavourSelect[i].checked)
    {
    alert("You picked " +
    document.displayForm.flavourSelect[i].value);
    }
    }
    }
    </script>
    </head>
    <body>
    <form name="displayForm" action="submit.php" method="post"> Pick a
    flavour: <br> <input type="checkbox" name="flavourSelect"
    value="Strawberry" onClick="displayFlavour()">Strawberry
    <br>
    <input type="checkbox" name="flavourSelect" value="Raspberry"
    onClick="displayFlavour()">Raspberry
    <br>
    <input type="checkbox" name="flavourSelect" value="Chocolate"
    onClick="displayFlavour()">Chocolate
    </form>
    </body>
    </html>

    More JavaScript Articles
    More By Team Melonfire, (c) Melonfire


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway