JQuery basics

From TOI-Pedia

about jQuery

jQuery is a Javascript library developed to make writing Javascript easy: "Write less, do more". Javascript allows you add functionality and interaction to your webdesign.
This page introduces the concept of the jQuery library and explains how to use existing documentation and refers to useful tutorials.

(add example)


concept

jQuery simplifies writing Javascript. The concept is best introduced by trying it out yourself. To get started download jQuery and load the jQuery script into your webpage by adding a single line in the head of your page:

  1. <html>
  2.   <head>
  3.     <script type="text/javascript" src="js/jquery.js"></script>
  4.   </head>
  5.   <body>
  6.     <p>your webpage content goes here</p>
  7.   </body>
  8. </html>

In the added highlighted code the 'js' in 'src' refers to the folder of the file and 'jquery.js' should be the name of the jQuery file itself.


To start using the jQuery library in your own webpage first create your own javascript and also refer to it in your webpage:

  1.   <head>
  2.     <script type="text/javascript" src="js/jquery.js"></script>
  3.     <script type="text/javascript" src="js/myscript.js"></script>
  4.   </head>

The second step is setting up your own script, in the example 'myscript.js', so that you can use jQuery functions, by adding the following code to your script:

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

Now we can start adding interactivity to our content by writing our own code. We could for instance add an action like animating the paragraph (<p> tag) in the example when hovering over it:

  1.  $(document).ready(function(){
  2.    $('p').hover(
  3.       function () {
  4.         $(this).animate({ 
  5.           marginLeft:"200px"
  6.         }, 1500 );
  7.       }, 
  8.       function () {
  9.         $(this).animate({ 
  10.           marginLeft:"0px"
  11.         }, 500 );
  12.       }
  13.    );
  14.  });

To understand the code we have to look at it in more detail:

....


For more information on the concept see the How jQuery works tutorial.


documentation

All official jQuery documentation can be found at docs.jquery.com.

When getting started the hardest part is finding out where the find what you are looking for. Therefore we will shortly describe each category (of the API Reference, as that's what you'll be using) in the documentation:

Often used:

  • Selectors: What element to add interaction to?
  • Events: Having control over when interaction
  • Effects: Making elements appear and disappear, move them, animate them
  • CSS: Add or remove css

Sometimes used

  • jQuery Core: The jQuery Core
  • Attributes: Modify HTML attributes
  • Traversing: Traversing is about adding selectors, geting parent elements, or children of the selected elements.
  • Manipulation: Deleting elements, adding new html, modifing existing html

Less used:

  • Ajax: Ajax helps you load new pages 'on the fly' without loading new pages
  • Utilities: jQuery utilities offer programmers more easy

tutorials

A list of basic tutorials can be found at docs.jquery.com/tutorials.

Personal tools
Actions
Navigation
Tools