Almost everything on a webpage is an "object". Almost all objects can respond to an event. You can control what an object does in response to an event by creating an event handler. In this article, you'll learn four different ways to connect an event handler to an event.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site

Four Ways to Use Java Script Event Handlers

On a webpage you might find buttons, checkboxes, links, text, and so on. Almost everything on a webpage is an "object". For example, text may be contained in paragraph object. Almost all objects can respond to an event. An event can be triggered by the user. For example, an onClick event is triggered when the user clicks on a button. An event can also be triggered by a browser action. For example, an onLoad event is triggered when a webpage completes loading.

You can control what an object does in response to an event by creating an event handler. An event handler might be a JavaScript function that performs some action, such as display a message box. You make objects on a webpage respond to events by connecting an event handler to an event. In this article, you'll learn four different ways to connect an event handler to an event.

The most common method to connect an event to an event handler is to use an HTML event attribute. For example, the html code below creates an html span object containing the text "Click Me".

<html>
<head>
</head>
<body>

<span onClick="popupBox()">Click Me</span>

<script language="JavaScript">
function popupBox()
{
alert("Hello There!");
}
</script>

</body>
</html>

The code above also creates a JavaScript code block containing an event handler function named "popupBox". When executed, the function creates a message box displaying the message "Hello There!".

The attribute onClick="popupBox()" in the span tag connects the "popupBox" event handler to the htmls span tag's onClick event. When you open the webpage in your browser and click on the text, a message box appears displaying the message "Hello There!".

It's more common to connect an event handler to the onClick event of a button object, but a span object and most other html objects also contain the onClick event. The above example shows a quick way to create a button on a webpage that is not associated with a form object.

A second method to connect an event handler to an event is in the JavaScript code. To do this, you need to give the span an id attribute as shown below.

<span id="mybutton">Click Me</span>

Then add the line document.getElementById("mybutton").onclick = popupBox; to the JavaScript code block as shown below. (Note that the event name onclick must use all small letters.)

<script language="JavaScript">
function popupBox()
{
alert("Hello There!");
}
document.getElementById("mybutton").onclick = popupBox;
</script>

Again, when you open the webpage in your browser and click on the text, a message box appears displaying the message "Hello There!".

If the object was contained within a form (for example a button on a form), you could address it using the line shown below.

document.formName.buttonName.onclick = popupBox;

If the object is the browser window itself, you could use the line shown below.

window.onscroll = popupBox;

Note: The window object doesn't have an onclick event. To test this example, you'll need to have enough text on the webpage to cause a scrollbar to appear.

Web Design Sections

RSS Feed RSS Feed

Easy Java Script Code
Easy Slide Show Code
Easy Slide Show Code with Mouseover Pause
Easy Slide Show Code With Linked Slides
Slide Show with Different Size Images
Easy Java Script Timer Code
Easy Java Script Windows
Easy Rollovers
Easy Picture Transition Effects
Easy Moving Popup Code
Easy Picture Zoom Code
Easy Picture Panning Code
Easy Java Script Animation
Easy JavaScript Picture Selector Code
Easy Animated Banners with Java Script
Easy Java Script Form Validation
Easy Drag n Drop Code
Easy Graph Generating Code
Easy Code to Sort a Table by Column
Easy Expanding Menu Code
Web Designer's Reference
Object-Oriented JavaScript
Easy Fading Text Banner Code
Easy Expanding Banner Code
Calendars for Your Website
Date Picker For Your Web Site
Calculators For Your Web Site
Loan Payment Calculator
Length Units Converter
Body Mass Index
Fahrenheit to Celsius Converter
Decimal to Hexidecimal Converter
Easy Code for Screen Tape Adding Machine
Code for Java Script Circle⁄Sphere Calculator
Code for Java Script Cube⁄Box Volume Calculator
Round a Float to 4 Digits to the Right of the Decimal Point
Disable IE8 Accelerators on Your Website
Prevent Automated Form Submissions With Java Script
Make Your Own Graphical Digital Clock
Let Your Web site Visitors Set Font Size
How to Disable the Browser Back Button
Add More Bang to Your Content With Keyword Popup Menus
Put a Music Off Switch on Your Webpage
Java Script Random Password Generator
Password Protection Using the JavaScript Cookie Method
Password Protection Using the Frames Method
Replace Drop-down with Drag-and-drop
Submit Forms Without CGI
Code for a Less annoying Popup Window
Java Script Comparison Operators
Using the Java Script Array Object
Using the Java Script Date Object
Java Script Message Boxes
Java Script Trim Function
Convert Mixed Number to Decimal
How to Shuffle the Deck With Java Script
Web Site Menus : Which Section Am I In?
How Far Did the User Scroll?
Where Did the User Click?
Four Ways to Use Java Script Event Handlers
Create Your Own Database Using Only Notepad : CDV


[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2009 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269