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:

    ...Often the accurate answer to a usage question begins, “It depends.” And what it depends on most often is where you are, who you are, who your listeners or readers are, and what your purpose in speaking or writing is.
    Kenneth G. Wilson (b. 1923)

    The gothic is singular in this; one seems easily at home in the renaissance; one is not too strange in the Byzantine; as for the Roman, it is ourselves; and we could walk blindfolded through every chink and cranny of the Greek mind; all these styles seem modern when we come close to them; but the gothic gets away.
    Henry Brooks Adams (1838–1918)