Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

jQuery

0.00/5 (No votes)
4 Jan 2010 1  
What is jQuery?jQuery is a light weight javascript library which provides fast and easy way of HTML DOM traversing and manipulation, event handling,

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

What is jQuery?

jQuery is a light weight javascript library which provides fast and easy way of HTML DOM traversing and manipulation, event handling, client side animations, etc. One of the greatest features of jQuery is, it supports an efficient way to implement AJAX applications because of its light weight nature. According to the jQuery official site, “jQuery is designed to change the way that you write JavaScript.”

Jquery basic example:
<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>

    <script type="text/javascript">
     
    </script>
  </head>
  <body>
    <a href="http://jquery.com/">jQuery</a>
  </body>
</html>

What is dollar sign($)?

In jQuery, the most powerful character / symbol is the dollar sign. A $() function normally returns a set of objects followed by a chain of operations. An example
view sourceprint?
1.$("div.test").add("p.quote").html("a little test").fadeOut();

Think of it as a long sentence with punctuations. Indeed it is a chain of instructions to tell the browser to do the following:

   1. Get a div with class name is test;
   2. Insert a paragraph with class name is quote;
   3. Add a little text to the paragraph;
   4. Operate on the DIV using a predefined method called fadeout.

Document.Ready()

The most commonly used command in jQuery is Document.Ready(). It makes sure code is executed only when a page is fully loaded. We often place code blocks inside this Document.Ready() event. For example:

  $(document).ready(function(){
   $("#buttonTest").click(function(event){
      alert("I am ready!");
   });
  });

More example to understand

jQuery has a simple statement that checks the document and waits until it's ready to be manipulated, known as the ready event:

 $(document).ready(function(){
   // Your code here
 });

Inside the ready event, add a click handler to the link:

 $(document).ready(function(){
   $("a").click(function(event){
     alert("Thanks for visiting!");
   });
 });

Save your HTML file and reload the test page in your browser. Clicking the link on the page should make a browser's alert pop-up, before leaving to go to the main jQuery page.

For click and most other events, you can prevent the default behaviour - here, following the link to jquery.com - by calling event.preventDefault() in the event handler:

 $(document).ready(function(){
   $("a").click(function(event){
     alert("As you can see, the link no longer took you to jquery.com");
     event.preventDefault();
   });
 });

Where to find more information?

Start out by checking the official site: http://docs.jquery.com/.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here