JavaScript onclick Event Handler

An event handler is an intermediary between user interaction with the website and a behavior executed by JavaScript based on the user interaction. In nearly all cases a user has to engage with the web page's user interface to initiate an onclick or other related event handler; because with the move to rich, dynamic website experiences, user experience is key.

There are two places an event handler can be found.

 

onclick Images as Buttons

This tutorial will explain the onclick event handler and one rudimentary use of events to turn brochure-style websites into dynamic solutions.

In the example below, the images have an onclick event handler included in the mark-up to turn the images into buttons when a user clicks on them.

Click on an image to preview an onclick event handler demonstration.

no image no image
Code Example
  1. function click(imageNumber){
  2. var userName=prompt("What is your name?","");
  3. alert("Hey " + userName + " you clicked on image " + imageNumber); }