JavaScript Link Object : onclick Event handler

Description

The onclick event handler of a link object can be defined in a piece of text, image or predefined area. It executes some JavaScript code or function when the pointing device (for example mouse) button is clicked over a link object.

Version

Implemented in JavaScript 1.0

Syntax

onclick = "JavascriptCode"

Parameter

JavascriptCode : JavaScript statement or JavaScript function.

Example of Link object : onclick event handler

In the following web document onclick event handler executes an alert box when the mouse clicked over the link object.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>JavaScript link object Event handler : onclick example</title>
</head>
<body>
<h1 style="color: red">JavaScript Link's Event handler : onclick</h1>
<h3>Click on Google or Yahoo .... and see the onclick effect ....</h3>
<hr />
<form name="form1" action="#">
<a href="http://www.google.com"
onclick='alert("You clicked on Google")'>
Google</a>
<br />
<br />
<a href="http://www.yahoo.com"
onclick='alert("You clicked on Yahoo")'>
Yahoo</a>
</form>
</body>
</html>

View the example of link object's onclick event handler in the browser

Practice link object's onclick event handler with Online Editor

Supported Browser

Internet Explorer 7 Firefox 3.6 Google Chrome 7 Safari 5.0.1 Opera 10
Yes Yes Yes Yes Yes

See also :

Javascript event handlers.

Javascript object's event handlers.

Please Google+, Like this tutorial on FaceBook, Tweet, save it as bookmark and subscribe with our Feed. Have suggestions? comment using Disqus down this page. Thanks.


share with delicious share with facebook w3r rss  share with digg share with reddit share with stumbleupon share with twitter