Thursday, August 26, 2010

jQuery($) Object Magic

My last post covered basics of jQuery. Taking it to another level, in this post I will be covering magic of jQuery(alias $) object which is very important one considering the usage, flexibility, simplicity of $ object .
The different ways using $ object are:
1) $(selector,[context]) 
2) $(html,[containing document])
3) $(html,properties)

Let me cover it one by one. We will be considering only one example for this to ease the understanding as follows:

<html><head>  
<title>
         First Jquery Example
<title><script type="text/javascript" src="jquery.js"></script>
</head>
 <body>
   <h2>Welcom to the world of jquery.</h2>
<div id="firstDiv">
<input type="button" id="clickMeButton1" value="Click Me" class="buttonClass"/>
</div>
<div id="secondDiv">
<input type="button" id="clickMeButton2" value="Click Me" class="buttonClass"/>
</div>
</body>
 </html>
 
1) $(selector,[context]) 
    selector: selector can be any valid string which matches to a set of elements. Selector in jQuery is simply a way to select some set of elements from DOM to have further processing on it like attaching click event changing CSS of that set of elements

                  For example:  In the code given above, we have a button having id as 'clickButton1'. So, we can select the same with this code:

          $("#clickButton1")          //returns button having id as 'clickButton1'
          $(".buttonClass")             //returns two button having id as 'clickButton1' and 'clickButton2'
          $("#clickButton1").click(function(){alert('hi')});          //apply a click event on the same button 

  selector is itself is a big topic to cover that can be taken in next post. For now this is just a way to select an element.

   context: This is an optional parameter we pass to jQuery object to differentiate between two or more than two elements return by a jQuery object. For example:

        $(".buttonClass")                    //returns two buttons having id as 'clickButton1' and 'clickButton2'
        $(".buttonClass",'#firstDiv')      //   returns one button having id as 'clickButton1'

  so just providing a context to it makes a lot of difference.

            some other forms of $ object are like:
            a) $(jQuery Object) : which makes a clone of jQuery Object we pass as parameter. Both of them refer to same object.
             b) $() : returns an empty object.

2) $(html, [containing document]):
    here html is a valid string containing proper tags. Using this we can add an element directly to some other element like this:

      $("<input type="TextBox" id="firstText"/>").appendTo('body');
            This will add text box on the fly to the end of body tag.

3) $(html, properties):
    here html is a valid string having single, standalone HTML element(for example <input/>, <span/>). Standalone here means that it should not have any attribute. We can add attribute by using properties parameters like this:

     $('<div/>',
          {id:'newDiv',
           class:'newDivClass'
           }) .appendTo('body');

Here a div element is attached to body. Here we can note that {} represents an object in javascript, similar to [] which returns an object of array.

This is a brief introduction to $ object. In this I tied to cover it as simple as possible. In the next post I will covered selector in jQuery. Please provide your valuable comments to improve my understanding with the topic.
    

Thursday, August 12, 2010

jQuery-Write Less Do More

Hi all, this is my first blog. I am highly intreseted in technical stuff and here to learn and share my thoughts. So, please provide feedback, comments or whatever you want. You can like it or kick it as well.

People must have heard about few emerging technologies. When we talk about people, we are refering to those who have interest in all these.

With this blog I am going to discuss one of the most emerging technolgies...JQuery.
I am sharing my thoughts here with taking a very basic introduction and how it works.

JQuery is a very fast javascript library that have a lot of advantages over traditional programming using javascript. Few of them are listed here:
1) Event handling,
2) Cross-browser indepedent,
3) Ajax implementation(the most imp),
4) Animation and many more....

Here I am sharing a simple example, which will give an alert message on click of a button and hopefully it may give a brief overview of jQuery.
First we have one html page having nothing but the jQuery file required to make our application compatible to it and name it as jquery.js.  
<html>
<head>
   <title>
         First Jquery Example
   </title>
<script type="text/javascript" src="jquery.js"></script>
</head>

<body>
   <h2>Welcom to the world of jquery.</h2>
<input type="button" id="clickMeButton" value="Click Me">
</body>
 </html>

It completes our HTML part. This code is not having javascript part where we give alert message. Lets have code for this:

<script type="text/javascript" >
 $(document).ready(function(){
   $("#clickMeButton").click(function(){
   alert("hi buddy!");
   
});

});
</script>

Wait- wait -wait....
I will explain lines one by one...

1)$():
   $ is the representation of jQuery objet. That means $() will return you an jQuery object.

2)  $(document).ready(function(){}):
      In our traditional javascript programming, we generally write like this:
      window.onLoad=function(){ alert("hi buddy!"); }
      problem with this code is, java script code doesn't run until all images, ads and banners finished loading and we have to put behavioural markup in html. With $(document).ready(function(){}) this is not the case. Whatever is inside this, get fired before window loads. so it solves our above problems with traditional javascript code. This gives us the fastest effect i.e., as soon as DOM get registered in browser we get our things done(which also gives us some nice animations in our page).

2) $("#clickMeButton").click(function(){});
Here we select the button with its ID. This is the syntax for selecting control.