Home arrow JavaScript arrow Page 3 - Introducing the jQuery JavaScript Library
JAVASCRIPT

Introducing the jQuery JavaScript Library


Welcome to the first part of a seven-part series on the jQuery JavaScript library. In this article I will provide you with a quick overview of how to use the library, including its helpful $() function and the ready() method. The jQuery library allows you to build unobtrusive client-side applications with only minor effort, and its syntax is simple to grasp.

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 4
August 12, 2009
TABLE OF CONTENTS:
  1. · Introducing the jQuery JavaScript Library
  2. · Getting started using jQuery
  3. · More examples of the $() function and the click() method
  4. · Firing up a function with a mousedown event

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Introducing the jQuery JavaScript Library - More examples of the $() function and the click() method
(Page 3 of 4 )

As you learned in the previous section, displaying an alert box when a link is clicked is actually a straightforward process thanks to the use of the $() function and the "ready()" method provided by the "jQuery" package.

However, let me go one step further and show you another practical example. It will show how to use the method to display a confirm box before redirecting users to Dev Shed's main page.

Here's the code sample that performs the aforementioned task:

 

<!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 onclick 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").click(function(){

return confirm('Do you want to visit Devshed.com now?');

});

});

</script>

</head>

<body>

<h1>Basic example on using jQuery with onclick event</h1>

<a href="http://www.devshed.com/">Visit Devshed.com now!</a>

</body>

</html>

 

That was easy to code and read, wasn't it? In this specific case, the $() function and the pertinent "ready()" method are used in conjunction to display a trivial confirm box each time a user clicks on the link that points to "Devshed.com." As you can see, whether the visitor is taken directly to the Dev Shed web site or not depends on the value returned by the "confirm" statement.

So far, so good. At this stage, I demonstrated how to use two key components of the "jQuery" library, that is the $() function and the "ready()" method respectively, to display a couple of message boxes each time a link is clicked by a hypothetical user.

In addition, although all of the practical examples developed so far are fairly simple, they're good enough to illustrate how to assign an event handler to a targeted element within a web document.

However, the "jQuery" library permits you to work easily with many mouse events, not just simple clicks. Therefore, the last section of this introductory tutorial will be focused on demonstrating how to use the library to fire up a function when the mouse is clicked over a web page link.

To learn more about how this will be achieved, you'll have to visit the following section. It's only one click away.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials