Home arrow JavaScript arrow Page 7 - Understanding The JavaScript Event Model (part 1)

Flavour Of The Month - JavaScript

This may be news to you, but JavaScript comes with a powerfuland flexible event model, one which provides developers with astandardized way of trapping and handling client-side events likekeystrokes and mouse clicks. This two-part article takes an in-depthlook at how this event model works, demonstrating some practical (andnot-so-practical) uses of the most common event handlers.

TABLE OF CONTENTS:
  1. Understanding The JavaScript Event Model (part 1)
  2. Popeye() And Olive()
  3. Handling Things
  4. Red Alert
  5. Mouse Hunt
  6. Forty Two
  7. Flavour Of The Month
  8. Linking Up
  9. Game Over
By: Team Melonfire, (c) Melonfire
Rating: starstarstarstarstar / 36
June 25, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVASCRIPT ARTICLES

- Javascript for Beginners: An Introduction
- Introduction to JavaScript
- Adding Elements to a Tree with TreeView jQue...
- Using the Persist Argument in a TreeView jQu...
- Using Unique and Toggle in a TreeView jQuery...
- Using Event Delegation for Mouseover Events ...
- Using the Animate Option in a Treeview jQuer...
- Using HTML Lists with Event Delegation in Ja...
- Opened and Closed Branches on a TreeView jQu...
- Mouseover Events and Event Delegation in Jav...
- Creating a TreeView JQuery Hierarchical Navi...
- Event Delegation in JavaScript
- A Look at the New YUI Carousel Control
- Working with Draggable Elements and Transpar...
- Displaying Pinned Handles with Resizable Con...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: