More Mouse and Keyboard Events with the jQuery JavaScript Library - Review: mouse overs and keydown events with jQuery
(Page 2 of 4 )
In case you haven't had the chance to read the previous tutorial of the series, where I explained how to handle mouse overs and keydown events with the jQuery library, below I listed a couple of examples that show how to perform these specific tasks. Here they are:
(example on handling a 'mouseover' event with jQuery)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Basic example on using jQuery with mouseover event</title>
<style type="text/css">
body{
padding: 0;
margin: 0;
background: #fff;
}
h1{
font: 24px bold Arial, Helvetica, sans-serif;
color:#000;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a").mouseover(function(){
alert('Redirecting to Devshed.com now!');
});
});
</script>
</head>
<body>
<h1>Basic example on using jQuery with mouseover event</h1>
<a href="http://www.devshed.com/">Visit Devshed.com now!</a>
</body>
</html>
(example on handling a 'keydown' event with jQuery)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Basic example on using jQuery with keydown event</title>
<style type="text/css">
body{
padding: 0;
margin: 0;
background: #fff;
}
h1{
font: 24px bold Arial, Helvetica, sans-serif;
color:#000;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("a").keydown(function(){
alert('Redirecting to Devshed.com now!');
});
});
</script>
</head>
<body>
<h1>Basic example on using jQuery with keydown event</h1>
<a href="http://www.devshed.com/">Visit Devshed.com now!</a>
</body>
</html>
These were really simple to code and grasp, right? As you can see, the pair of examples listed above demonstrate in a nutshell how to build basic JavaScript applications that respond to mouse overs and keydown events. Of course, in this case, the applications will display a couple of alert boxes on screen in response to those events. This simple concept, however, can be easily applied when working with real-world projects.
So far, so good. At this point, you hopefully realized that handling mouse and keyboard events by way of the jQuery library is simple. So, what comes next? Well, as I mentioned in the introduction, in the section to come I'm going to provide you with another hands-on example to demonstrate how to process keypress events by using a brand new method of the jQuery package.
To see how this will be achieved, please click on the link that appears below and keep reading.
Next: Manipulating a keypress event with the keypress() method >>
More JavaScript Articles
More By Alejandro Gervasio