Monday, November 30, 2009

Introduction to Jquery

Introduction

JQuery is a Java script libarary/ Java Script framework that simplifies the interaction process or access process of traversing in HTML document. It provides methods to make animations, add ajax interaction to the page, provides an easy way to apply CSS to any items and provides an easy mechanism for binding and unbinding events. Huge code written using Java script can easily replaced by few lines of code in JQuery.

History of JQuery

Initially it’s was released in January 2006 but the very first stable version of JQuery 1.0 was released in August 2006. This version had support for CSS, events and Ajax. After that many version of JQuery were released but the latest version is JQuery 1.3.2. You can download this from JQuery website.

What can be done using JQuery

1. Allows to access elements in the document

If one need to access the DOM tree without any JavaScript libarary, one has to write many lines of code. JQuery provides a selector mechanism to access any part of DOM.

2. Easily apply CSS

As it’s known that CSS is the most powerful and mostly used for good appreance of any webpage. JQuery provides CSS Selectors which allows to change the CSS classes or individual style for any portion of the document. This can be done even after the page is rendered.

3. Make Animation

For better user experience, animation can do the job for you. JQuery provides many methods to perform animations like show,hide, fade, wipe, start, stop etc. Doing all this with JQuery is fun as No huge lines of code, no complex logic.

4. Ajax Interaction

In today’s world, Ajax is one of the most popular technology used in almost every website for better user experience. Jquery provides support for ajax also which allows to access server side event without refreshing the web page.

5. API to change document’s content

JQuery provides API (Application Program Interface) which allows to change the content of the document. Changing Text, inserting images, ordering of list can be done with few keystrokes with JQuery API. Entire structure of HTML can be rewritten or extended.

6. Event handling

Any technology is a failure if it cannot responsed to the user when any event takes place. Jquerys’s event handling is one the decent feature. It quickly responsed to any event such as user clicking a button.

Demo

To start with JQuery, first download the Jquery from it’s official website (http://JQuery.com). Make sure you download the latest copy of the JQuery.

1<script src="Script/jquery.js" type="text/javascript"></script>

I have copied the jquery.js and placed it in script directory of the project. In this demo, I am going to show you how easily you can change the CSS and do animation.

Let’s first take a look at HTML

1<h2>
2History of JQuery</h2>
3<div id="content">
4Initially it's was released in January 2006 but the very first stable version of JQuery 1.0 was released in August 2006. This version had support for CSS,events and Ajax. After that many version of JQuery were released but the latest version is JQuery 1.3.2. You can download this from JQuery website.
5</div>

As you can see, I have placed a div tag with it’s ID set to content. Now, I will show you how you can find the div and apply CSS to it using JQuery.

01<style type="text/css">
02    .ApplyColor
03        {
04            background-color: Gray;
05            font-family: Arial;
06            font-size: 10pt;
07            color: White;
08        }
09    h2
10        {
11            font-size: 20pt;
12        }
13</style>
14 
15<script type="text/javascript" language="javascript">
16 
17    $(document).ready(function() {
18        $('#content').addClass('ApplyColor');
19    });
20</script>

.ready() is a jQuery event handler. This particular event handler will execute when the document is ready to be accessed and scripts have completed loading. A .ready() handler can be placed anywhere on the page and you can even have multiple ready handlers in the page.

Now, if you run this page, you will see the div with content id is having gray background and white color foreground.

Let’s go to the animation part with JQuery.

Place a button and some text in Paragraph tag. Using JQuery, I will add a click event handler.

1<asp:Button ID="btnShow" runat="server" Text="Show" />
2<p style="display: none;background-color:Red">
3    Hello
4</p>

Now add this Jquery code to document.ready event

01<script type="text/javascript" language="javascript">
02 
03    $(document).ready(function() {
04        $('#btnShow').click(function() {
05              $("p").show("");
06              return false;
07        });
08    });
09 
10</script>

Above code will find the btnshow and add a click event handler to it. When the button is click then Jquery will find the p tag and make a call to show function which will display the content of the p tag on the screen.

Like wise there are many more functions for animation. You can find the whole list over here.

Reference

http://jquery.com/

Conclusion

This is just an overview of starting with Jquery. Many more complex things can be done via jquery with ease. Go to this main page of Jqueryand learn as much as you can.

Happy Programming…

Virendra Dugar :)

Blogged with the Flock Browser

No comments:

back to top