Wednesday, 16 October 2013

JQUERY WORKING PROCESS - MANOHAR GOUD 9866550012

JQUERY WORKING PROCESS

jQuery is the most popular JavaScript library nowadays. It uses CSS selector style syntax to fetch elements in document object model (DOM) into wrapped element set, and then manipulate elements in the wrapped set with jQuery functions to archive different effect. Though the way of using jQuery is very easy and intuitive, we should still understand what is happening behind the scene to better master this JavaScript library.

Basic Concepts in jQuery

There are some basic concepts in jQuery should be known before we dig into the jQuery library

$/jQuery object and $()/jQuery() function

The $/jQuery object is a global object through which all jQuery functions are accessed. 
It is also a function object, so the most common approach uses it is via $()/jQuery() function. $()/jQuery() function can be used to select a group of elements in DOM. It is also known as wrapper function. One simple example of using $()/jQuery() function is this 

$(“#ABC”); 
or 
jQuery(“#ABC”); 

The parameter passed into $()/jQuery() function is selector. Selector is an expression that has CSS selector style syntax. In above example, I am trying to select elements that have id equal to #ABC.

Wrapped set

Wrapped set is an array-like structure that contains all the selected DOM elements. The above $(“#ABC”) returns back a wrapped set. You can iterate over the wrapped set like an array or access individual elements via indexer. More importantly, you can apply jQuery functions against all the selected elements.

How the jQuery $()/jQuery() function works behind the scene

Because majority of jQuery function calls are started with $()/jQuery() function, so we need to understand what happening inside of it. Before go into $()/jQuery(), let’s take a look where $/jQuery object is defined. The $/jQuery object is the access point to jQuery functions. It’s a global function variable defined in jQuery. Here is the line of source code in jQuery defines it
// Expose jQuery to the global object
window.jQuery = window.$ = jQuery;

The window object represents an open window in a browser, by putting $/jQuery right under window, it is defined as a global object that can be accessed in current open window.

But, what is the “jQuery” used at the end of above line of source code? It is declared in the beginning of jQuery library

var jQuery = (function() {

All the magic are happening inside of jQuery object declaration expression, but if look into it directly, you will get lost, so let me simplify it before we continue.

The simplified version jQuery library source code
Caveat: the simplified version is only for research purpose. It doesn’t really have all fundamental features jQuery provides. Never use it in real project.

var jQuery = (function ()
{
// Define a local copy of “k”
var k = function (selector, context)
{
// The k object is actually just the init constructor 'enhanced'
var kobj = new k.fn.init(selector, context);
return kobj;
};
//Define k’s fn prototype, specially contains init method
k.fn = k.prototype = {
init: function (selector, context)
{
if (!selector)
{
return this;
}
}
};
// Give the init function the “k” prototype for later instantiation
k.fn.init.prototype = k.fn;
// Return “k” to the global object
return k;
})();

From the above source code, you can see jQuery function variable is defined and assigned as result of an anonymous function call.


Sidebar: How to define anonymous function

In JavaScript, you can define an anonymous function and call it right away.

For example,
(function () { alert(“hello world”); })();
By putting function literal
function() { alert(“hello word”); }
into parentheses
(function() { alert(“hello world”); })
You can immediately invoke it with outside parentheses


Inside of the anonymous function, a function k is defined, it has signature like this 

function (selector, context)

The first parameter is selector and the second parameter is context. In original jQuery source code, function k is actually named as jQuery that can be confused with the outer most jQuery function variable.

Inside of function k, a new instance of init function class is created and returned. The init function class is defined later on as k’s prototype method. It is a JavaScript style “class”.

After the function k declaration, an anonymous function class is defined as k’s prototype. Prototype is a special property on JavaScript’s function class that is used to point to another function object. So, all the instances of that function class will have access to member functions defined in the prototype function class. In here, init method is available by all instance of k function class. The init method has signature like this

function (selector, context)

The k’s prototype has init member method, in the meantime, init function’s prototype is assigned with k function’s prototype. This is a circular reference that usually should be avoided in normal programming practice, but, it’s the way jQuery make the return of init function can have access to all the methods defined on k.


Sidebar: Function is the first class object in Javascript

JavaScript is a functional programming style language, but the function is not a pure function in our traditional definition. The function can be created on fly, can have properties and methods, and can be passed to other functions. If you want to invoke the JavaScript function in traditional way, you can have something like this
function display()
{
  alert(“hello world”);
}

display();
If you want to use function as class, then you can have something like this
function Car(model)
{
  this.Model = model;

  this.Drive = function() { alert(“Go”) }  
}

var car = new Car(“Ford”);
So, the function statement is actually used to define a Car “class”. The Car class has a property Model that is initialized with parameter in function class constructor and a Drive method. So, you can create as many instance of Car class as you want. However, JavaScript’s class is not a true class because it doesn’t really have the three key characters of class in OO language.


The way k’s prototype function defined is called function literal. It is used widely in jQuery for different purposes.


Sidebar: Different ways defining function in JavaScript

1. Use function statement to define a function
function open() {
  //open logic
}
2. Use function literal to define a function
The simplest function you can define with literal is like this
var o = { }
also, you can use literal to define a function “class”
var apple = {
  type: “macintosh”,
  color: “red”,
  getInfo: function() {
    return this.color + ‘ ‘ + this.type + ‘ apple’;
  }
}
3. Use Function object to define a function
Function Object is JavaScript built-in Object. In JavaScript, I can do this
var add = new Function(a, b, “return a + b;”);
to define a function “add”. The Function will treat the first two parameters “a” and “b” as function parameters and the last parameter “return a + b” as function body.


After that, the internal k function is returned to outside variable jQuery. What the jQuery got is not just a k function, and also a closure that k function lived in.


Sidebar: What is closure in JavaScript

A closure is a context that created by JavaScript when returning the inside function to outside, so the inside function can still have access to those local variables.

An example of closure is like this
var addition = function(numberA) { return function(numberB) { alert (numberA + numberB); }}
so, you can create instance of addition with indicated numberA
var add2 = addition(2);
var add3 = addition(3);
add2(3); // it will display 5
add3(3); // it will display 6


At the end, k function is returned to the global variable jQuery. 

The intricate and complex design inside of jQuery library is to make extending and using this library simple and easy. You can attach custom function to jQuery.fn to add your own business logic. And the custom function can be invoked same as built-in function by wrapped set, such as this 



// Define a plugin function Test
jQuery.fn.Test = function () { alert("Test"); }
// Invoke Test function via jQuery() function
jQuery().Test();



















Tuesday, 8 October 2013

JQUERY SELECTORS FUNDAMENTAL EXAMPLES - MANOHAR GOUD 9866550012



<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("*").css("background-color","yellow");
});
</script>
</head>
<body>

<h1>Welcome to Manohar Gouds Blog</h1>
<p class="intro">My name is K MANOHAR GOUD.</p>
<p>I live in HYDERABAD - 9866550012 - k.manohar.goud@gmail.com.</p>
<p>I like Teaching</p>

<p>Who is your favourite:</p>
<ul id="choose">
<li>Goofy</li>
<li>Mickey</li>
<li>Pluto</li>
</ul>

</body>
</html>



<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("#intro").css("background-color","yellow");
});
</script>
</head>
<body>

<h1>Welcome to Manohar Goud's Blog</h1>
<p id="intro">My name is K Manohar Goud.</p>
<p>I live in HYDERABAD - 9866550012 - k.manohar.goud@gmail.com.</p>

</body>
</html>


<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $(".intro").css("background-color","yellow");
});
</script>
</head>
<body>

<h1>Welcome to Manohar Gouds Blog</h1>
<p class="intro">My name is K Manohar goud</p>
<p>I live in Hyderabad - 9866550012 - k.manohar.goud@gmail.com</p>


</body>
</html>


<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $(".intro,.demo,.end").css("background-color","yellow");
});
</script>
</head>
<body>

<h1>Welcome to Manohar Goud's Blog</h1>
<p class="intro">This paragraph has class "intro". - Manohar Goud</p>
<p>This is a paragraph.</p>
<p class="demo">This paragraph has class "demo". -  Manohar Goud</p>
<p>This is another paragraph.</p>
<p class="end">This paragraph has class "end". - Manohar Goud</p>

</body>
</html>



<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("p").css("background-color","yellow");
});
</script>
</head>
<body>

<h1>Welcome to Manohar Goud's Blog</h1>
<p class="intro">My name is K. Manohar Goud</p>
<p>I live in Hyderabad - 9866550012 - k.manohar.goud@gmail.com</p>
<p>I like Teaching</p>

Who is your favourite:
<ul id="choose">
<li>Goofy</li>
<li>Mickey</li>
<li>Pluto</li>
</ul>

</body>
</html>


<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("h2,div,span").css("background-color","yellow");
});
</script>
</head>
<body>

<h1>Welcome to Manohar Gouds Blog</h1>
<h2>Nice to meet you</h2>
<div>Very nice indeed.</div>
<p>How are you?</p>
<p>I'm fine, <span>thanks.</span></p>

</body>
</html>




















































































JQUERY FUNDAMENTALS - MANOHAR GOUD


JQUERY SYNTAX

SelectorExampleSelects
*$("*")All elements
#id$("#lastname")The element with id="lastname"
.class$(".intro")All elements with class="intro"
.class,.class$(".intro,.demo")All elements with the class "intro" or "demo"
element$("p")All <p> elements
el1,el2,el3$("h1,div,p")All <h1>, <div> and <p> elements
   
:first$("p:first")The first <p> element
:last$("p:last")The last <p> element
:even$("tr:even")All even <tr> elements
:odd$("tr:odd")All odd <tr> elements
   
:first-child$("p:first-child")All <p> elements that are the first child of their parent
:first-of-type$("p:first-of-type")All <p> elements that are the first <p> element of their parent
:last-child$("p:last-child")All <p> elements that are the last child of their parent
:last-of-type$("p:last-of-type")All <p> elements that are the last <p> element of their parent
:nth-child(n)$("p:nth-child(2)")All <p> elements that are the 2nd child of their parent
:nth-last-child(n)$("p:nth-last-child(2)")All <p> elements that are the 2nd child of their parent, counting from the last child
:nth-of-type(n)$("p:nth-of-type(2)")All <p> elements that are the 2nd <p> element of their parent
:nth-last-of-type(n)$("p:nth-last-of-type(2)")All <p> elements that are the 2nd <p> element of their parent, counting from the last child
:only-child$("p:only-child")All <p> elements that are the only child of their parent
:only-of-type$("p:only-of-type")All <p> elements that are the only child, of its type, of their parent
   
parent > child$("div > p")All <p> elements that are a direct child of a <div> element
parent descendant$("div p")All <p> elements that are descendants of a <div> element
element + next$("div + p")The <p> element that are next to each <div> elements
element ~ siblings$("div ~ p")All <p> elements that are siblings of a <div> element
   
:eq(index)$("ul li:eq(3)")The fourth element in a list (index starts at 0)
:gt(no)$("ul li:gt(3)")List elements with an index greater than 3
:lt(no)$("ul li:lt(3)")List elements with an index less than 3
:not(selector)$("input:not(:empty)")All input elements that are not empty
   
:header$(":header")All header elements <h1>, <h2> ...
:animated$(":animated")All animated elements
:focus$(":focus")The element that currently has focus
:contains(text)$(":contains('Hello')")All elements which contains the text "Hello"
:has(selector)$("div:has(p)")All <div> elements that have a <p> element
:empty$(":empty")All elements that are empty
:parent$(":parent")All elements that are a parent of another element
:hidden$("p:hidden")All hidden <p> elements
:visible$("table:visible")All visible tables
:root$(":root")The document’s root element
:lang(language)$("p:lang(de)")All <p> elements with a lang attribute value starting with "de"
   
[attribute]$("[href]")All elements with a href attribute
[attribute=value]$("[href='default.htm']")All elements with a href attribute value equal to "default.htm"
[attribute!=value]$("[href!='default.htm']")All elements with a href attribute value not equal to "default.htm"
[attribute$=value]$("[href$='.jpg']")All elements with a href attribute value ending with ".jpg"
[attribute|=value]$("[title|='Tomorrow']")All elements with a title attribute value equal to 'Tomorrow', or starting with 'Tomorrow' followed by a hyphen
[attribute^=value]$("[title^='Tom']")All elements with a title attribute value starting with "Tom"
[attribute~=value]$("[title~='hello']")All elements with a title attribute value containing the word "hello"
[attribute*=value]$("[title*='hello']")All elements with a title attribute value containing the string "hello"
   
:input$(":input")All input elements
:text$(":text")All input elements with type="text"
:password$(":password")All input elements with type="password"
:radio$(":radio")All input elements with type="radio"
:checkbox$(":checkbox")All input elements with type="checkbox"
:submit$(":submit")All input elements with type="submit"
:reset$(":reset")All input elements with type="reset"
:button$(":button")All input elements with type="button"
:image$(":image")All input elements with type="image"
:file$(":file")All input elements with type="file"
:enabled$(":enabled")All enabled input elements
:disabled$(":disabled")All disabled input elements
:selected$(":selected")All selected input elements
:checked$(":checked")All checked input elements















Sunday, 6 October 2013

JQUERY ACCORDION UI - MANOHAR GOUD




<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>K MANOHAR GOUD - 9866550012 - jQuery UI Accordion - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <script>
  $(function() {
    $( "#accordion" ).accordion();
  });
  </script>
</head>
<body>

<div id="accordion">
  <h3>Section 1</h3>
  <div>
    <p>
    Mauris mauris ante, blandit et, ultrices a, suscipit eget, quam. Integer
    ut neque. Vivamus nisi metus, molestie vel, gravida in, condimentum sit
    amet, nunc. Nam a nibh. Donec suscipit eros. Nam mi. Proin viverra leo ut
    odio. Curabitur malesuada. Vestibulum a velit eu ante scelerisque vulputate.
    </p>
  </div>
  <h3>Section 2</h3>
  <div>
    <p>
    Sed non urna. Donec et ante. Phasellus eu ligula. Vestibulum sit amet
    purus. Vivamus hendrerit, dolor at aliquet laoreet, mauris turpis porttitor
    velit, faucibus interdum tellus libero ac justo. Vivamus non quam. In
    suscipit faucibus urna.
    </p>
  </div>
  <h3>Section 3</h3>
  <div>
    <p>
    Nam enim risus, molestie et, porta ac, aliquam ac, risus. Quisque lobortis.
    Phasellus pellentesque purus in massa. Aenean in pede. Phasellus ac libero
    ac tellus pellentesque semper. Sed ac felis. Sed commodo, magna quis
    lacinia ornare, quam ante aliquam nisi, eu iaculis leo purus venenatis dui.
    </p>
    <ul>
      <li>List item one</li>
      <li>List item two</li>
      <li>List item three</li>
    </ul>
  </div>
  <h3>Section 4</h3>
  <div>
    <p>
    Cras dictum. Pellentesque habitant morbi tristique senectus et netus
    et malesuada fames ac turpis egestas. Vestibulum ante ipsum primis in
    faucibus orci luctus et ultrices posuere cubilia Curae; Aenean lacinia
    mauris vel est.
    </p>
    <p>
    Suspendisse eu nisl. Nullam ut libero. Integer dignissim consequat lectus.
    Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
    inceptos himenaeos.
    </p>
  </div>
</div>


</body>
</html>
















Wednesday, 2 October 2013

JQUERY SYNTAX FOR BEGGINERS MANOHAR GOUD



WHAT IS JQUERY ?

jQuery is a Javascript library, and it is a language designed to simplify the client-side scripts in the browser. It was created by John Resig at BarCamp New York in 2006. It is an open source software under licenses from MIT and GPL combined. Jquery 1.9.1 is the latest version disponible in the jquery official site .
jquery tutorial for beginners
You will find the slogan with the jQuery logo  : “Write less, Do more “.In fact, you can make nice effects with a few lines of jQuery code. Its syntax is very easy to understand.
As a  jQuery beginner ; code once you understand the basic concepts available in the next tutorials. jQuery is particularly suitable for:
         - Create animations.
         - Handle browser  events.
         - Loading Content using AJAX programs.
         - Create Awesome sliders like the one in ourHome page.
         - Build professional Mobile phone Applications.
         - …..

It also used in creating web pages and dynamic elements. It allows developers to create plugins that are compatible with a wide number of platforms such us : Adobe Dreamweaver,Wordpress, Joomla...etc.


Thousands of jQuery plugins are available on the web. Among the most common were assistants AJAX data grids, XML tools, drag and drop, the manipulators Cookies, etc.. You can find the best plugins in the jQuery official page.


What makes jQuery so powerful anduniversal ?

The JavaScript language was born in 1995. Since then, its implementation in differentbrowsers (Safari, Google chrome,Firefox...) on the market has made ​​a rather haphazard way: over the different versions (both language browsers), some features have been retained,others do not. Thus, a single instruction can be recognized JavaScript in a browser and not in another, even in some version of a browser and not in another. What a headache for programmers!
Fortunately, jQuery comes to the rescue: defining its own set of instructions, it acts as a wrapper for
different versions of JavaScript, whether existing or coming. On the other hand, it allowsbrowsers present on market, their multiple versions and their compatibility with theinstructions of JavaScript and AJAX.
For a script written in JavaScript to run correctly on different versions of each browser,
programmer-like you-must set up a series of tests and run a specific code for each browser and version , 
as shown in the following figure : 


Javascript browsers test



With jQuery, these tests are unnecessaryjust run the necessary instructions, regardless of the browser used, or version of JavaScript compatible with this browser.        
Where applicable, all these tests are made ​​so transparent . In your case, you only have toworry about the code. 

Remember, jQuery is very convenient, but do not enjoy and think only of the visualdevelopment : a page must foremost based on solid HTML bases !

But what happens when a new version of JavaScript born
Well, the jQuery  instructions are completed. Accordingly. You can continue to use the instructions already used and / or consult the documentation available on the new instructions. Whatever your approach, all used instructions work in all available browsers.This approach is a real bonus for you, whether beginner or experienced.
I will add some other details that will certainly convince you that you made ​​the right choiceby deciding to learn jQuery :
                        - The official documentation is available for everyone and in high quality.
                        - The Community that revolves around jQuery is expanding and providesquality support.
                        - Many leading players of the Web (Microsoft, Google, Amazon, Twitter,Mozilla, etc.), Use jQuery.
                       - A multitude of plugins available to enhance the basic capabilities of jQuery




JQUERY SYNTAX


Learn the basic syntax of jQuery which is realy the most important step when learning  any programming language.Unfortunatly , many beginners who wants to learn faster ignore this step and focus on advanced technics and complex functions , but they'll find many big problems because they don't know how this function's built and what does it mean this keyword , ...



Like any other Javascript library , jQuery was simply designed to change and interact the Html document . So , we have to understand the way that javascript could interact with Html , in other words, the DOM (Document Object Model)  .The image below represent a DOM structure for an Html document :


DOM Structure


The DOM define the way to access and manipulate Html Documents , Every node in Html can be easily accessed by Javascript and then jQuery .Don't worry , you will learn more about DOM interaction in the next tutorials .So , let's move to the  jQuery syntax.

Indeed , the process is easy to understand , jQuery access the Html or CSS elemnt using a 'Selector'  and then it perform an action .

A jQuery selector start with this symbol : $( )  ,  Inside the parentheses, we can put :

    -Html Tag name :      $('h1')   to access all the level 1 headers in the Html document.
    -Tag Class : The same way when we refer to a Html tag class in CSS using the point : . at the begin of the class name  . let's consider the following html code :

<p class = 'paragraph'></p>

to access this element using jQuery , simply add the tag class name inside the parentheses and preced it by a point :

$('.paragraph')

      -Tag Id : the same procedure for the class , just replace the point with #

<h1 id='header-1'>this is a header<h1> 

$('#header-1') 


Note : 

If you want to select all the elements in the document , just put this symbol : * inside the parentheses.
example : $('*')  
 



What is jQuery?

jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.

Who's Using jQuery

Other jQuery Foundation Projects

A Brief Look

DOM Traversal and Manipulation

Get the <button> element with the class 'continue' and change its HTML to 'Next Step...'
1
$( "button.continue" ).html( "Next Step..." )

Event Handling

Show the #banner-message element that is hidden with display:none in its CSS when any button in #button-container is clicked.
1
2
3
4
var hiddenBox = $( "#banner-message" );
$( "#button-container button" ).on( "click", function( event ) {
hiddenBox.show();
});

Ajax

Call a local script on the server /api/getWeather with the query parameter zipcode=97201 and replace the element #weather-temp's html with the returned text.
1
2
3
4
5
6
7
8
9
$.ajax({
url: "/api/getWeather",
data: {
zipcode: 97201
},
success: function( data ) {
$( "#weather-temp" ).html( "<strong>" + data + "</strong> degrees" );
}
});