Monday 5 May 2014

First Day: Basics and Introduction with jQuery

Hi all,

Its my very first post so pardon me.

I have been working with jquery for past three years and found guys having difficulty in understanding it, so I though why not help others with it.

This is official home page of jQuery and you can download jquery library from jQuery download.

I would recommend having minified version of jQuery to use, since its small in size will save user time as well.

Or you may use some CDN (Content Delivery Network for this work) as per your need.

There are two versions of jQuery available for downloading:
  • Production version - this is for your live website because it has been minified and compressed
  • Development version - this is for testing and development (uncompressed and readable code)

you may include jquery in your code as given below: 
<script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
OR
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
OR
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.0.min.js"></script> 
you may choose any version based on your need. I have worked on jquery version 1.9.1, so I would be following that in my code examples.

First thing I would like you to tell is jQuery is JavaScript library. When I say library that means it have almost functions which would save your work, it greatly simplies JavaScript programming. And its easy to learn just work around is needed.

Before you start with jQuery you need to have basic knowledge about these things:

1- HTML (HyperText Markup language) - Why?
     We would work with HTML for jQuery.

2- CSS (Cascading Style Sheet) - Why?
     We would use many selectors from CSS.

3- JavaScript - Why?
     Since jQuery is library of JavaScript we would need to understand basics of JavaScript.


What is jQuery?

jQuery is a lightweight, "write less, do more", JavaScript library with cross browser compatibility support, i.e., it will work same across all browsers including IE earlier versions like IE 6.

The purpose of jQuery is to make it much easier to use JavaScript on your website.

jQuery takes a lot of common tasks that require many lines of JavaScript code to accomplish, and wraps them into methods that you can call with a single line of code.

jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX(Asynchronous JavaScript and XML ) calls and DOM manipulation.

There are lots of plugin created using jQuery library, which are being used around internet website some of those plugins you might want to have are:
  • Image slider plugin
  • Pagination plugin
  • Form Validation  plugin
  • etc....

The jQuery library contains the following features:
  • HTML/DOM (Document Object Model) manipulation
  • CSS manipulation
  • HTML event methods
  • Effects and animations
  • AJAX (Asynchronous JavaScript and XML)
  • Utilities


Why jQuery?

There are lots of other JavaScript frameworks out there, but jQuery seems to be the most popular, and also the most extendable.
Many of the biggest companies on the Web use jQuery.


How jQuery works?

With jQuery you select (query) HTML elements and perform "actions" on them.

jQuery Syntax

The jQuery syntax is tailor made for selecting HTML elements and performing some action on the element(s).
Basic syntax is: $(selector).action()
  • A '$' sign to define/access jQuery
  • A (selector) to "query (or find)" HTML elements
  • A jQuery action() to be performed on the element(s)
  • '$' sign is alias of jQuery.
Examples:
$(this).hide() - hides the current element.
$("p").hide() - hides all <p> elements.
$(".test").hide() - hides all elements with class="test".
$("#test").hide() - hides the element with id="test".

Are you familiar with CSS selectors?

jQuery uses CSS syntax to select elements. You will learn more about the selector syntax in the next post of my blog.

The Document Ready Event

You might have noticed that all jQuery methods are inside a document ready event:

$(document).ready(function(){

   // jQuery methods go here...

}); 

This is to prevent any jQuery code from running before the document is finished loading (is ready).
It is good practice to wait for the document to be fully loaded and ready before working with it. This also allows you to have your JavaScript code before the body of your document, in the head section.
Here are some examples of actions that can fail if methods are run before the document is fully loaded:
  • Trying to hide an element that is not created yet
  • Trying to get the size of an image that is not loaded yet
Tip: The jQuery team has also created an even shorter method for the document ready event:

$(function(){

   // jQuery methods go here...

}); 

Use the syntax you prefer. We think that the document ready event is easier to understand when reading the code.

$(document).ready(function(){
  $("button").click(function(){
    $("p").hide();
  });
}); 


Working Example: Demo link

you can try these above function to check if your jQuery is working fine.

Happy Coding!!!



4 comments:

  1. Good One Mohan ... Its really helpful for the beginners of JQuery like me,,,
    i am waiting of your nxt ones....

    ReplyDelete
    Replies
    1. I am glad it helped you, I have already started working on my next post, will be posting it soon.

      Delete
  2. this is so good sir for beginners like me..thank u..n waiting 4 next one..

    ReplyDelete
  3. Easy to understand and start with jquery.

    ReplyDelete