JQuery - Usage Styles

Usage Styles

jQuery has two usage styles:

  • Via the $ function, which is a factory method for the jQuery object. These functions, often called commands, are chainable as they all return jQuery objects.
  • Via $.-prefixed functions. These are utility functions, which do not work on the jQuery object per se.

Typically, access to and manipulation of multiple DOM nodes begins with the $ function being called with a CSS selector string, which results in a jQuery object referencing matching elements in the HTML page. This node set can be manipulated by calling instance methods on the jQuery object, or on the nodes themselves. For example:

$("div.test").add("p.quote").addClass("blue").slideDown("slow");

This line finds the union of all div tags with class attribute test and all p tags with CSS class attribute quote, adds the class attribute blue to each matched element, and then increases their height with an animation. The $ and add functions affect the matched set, while the addClass and slideDown affect the referenced nodes.

The following script automatically checks whether the jQuery file is included. If not, it appends a jquery reference to the head section

if(!(window.jQuery && window.jQuery.fn.jquery == '1.6.2')) { var s = document.createElement('script'); s.setAttribute('src', 'http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js'); s.setAttribute('type', 'text/javascript'); document.getElementsByTagName('head').appendChild(s); }

The methods prefixed with $. are convenience methods or affect global properties and behaviour. For example, the following is an example of the map function called each in jQuery:

$.each(, function{ document.write(this + 1); });

This writes "234" to the document.

It is possible to perform browser-independent Ajax queries using $.ajax and associated methods to load and manipulate remote data.

$.ajax({ type: "POST", url: "example.php", data: "name=John&location=Boston" }).done( function(msg){ alert( "Data Saved: " + msg ); }).fail( function( xmlHttpRequest, statusText, errorThrown ) { alert( "Your form submission failed.\n\n" + "XML Http Request: " + JSON.stringify( xmlHttpRequest ) + ",\nStatus Text: " + statusText + ",\nError Thrown: " + errorThrown ); });

This example posts the data name=John and location=Boston to example.php on the server. When this request finishes successfully, the success function is called to alert the user.

Read more about this topic:  JQuery

Famous quotes containing the words usage and/or styles:

    Girls who put out are tramps. Girls who don’t are ladies. This is, however, a rather archaic usage of the word. Should one of you boys happen upon a girl who doesn’t put out, do not jump to the conclusion that you have found a lady. What you have probably found is a lesbian.
    Fran Lebowitz (b. 1951)

    ... it is use, and use alone, which leads one of us, tolerably trained to recognize any criterion of grace or any sense of the fitness of things, to tolerate ... the styles of dress to which we are more or less conforming every day of our lives. Fifty years hence they will seem to us as uncultivated as the nose-rings of the Hottentot seem today.
    Elizabeth Stuart Phelps (1844–1911)