jQuery, Features, Functions, Development

jQuery








User Review


5
(1 vote)


jQuery is a JavaScript library designed to simplify HTML DOM tree traversal and manipulation, as well as event handling, CSS animation, and Ajax.[rx] It is free, open-source software using the permissive MIT License.[rx] As of May 2019, jQuery is used by 73% of the 10 million most popular websites.[rx] Web analysis indicates that it is the most widely deployed JavaScript library by a large margin, having 3 to 4 times more usage than any other JavaScript library.[rx][rx]

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.

Features of jQuery

jQuery includes the following features:

  • DOM element selections using the multi-browser open source selector engine Sizzle, a spin-off of the jQuery project[rx]
  • DOM manipulation based on CSS selectors that uses elements’ names and attributes, such as id and class, as criteria to select nodes in the DOM
  • Events
  • Effects and animations
  • Ajax
  • Deferred and Promise objects to control asynchronous processing
  • JSON parsing
  • Extensibility through plug-ins
  • Utilities, such as feature detection
  • Compatibility methods that are natively available in modern browsers, but need fallbacks for older browsers, such as jQuery.inArray() and jQuery.each().
  • Cross-browser support

Functions of jQuery

  • jQuery provides two kinds of functions, static utility functions and jQuery object methods. Each has its own usage style.
  • Both are accessed through jQuery’s main identifier: jQuery. This identifier has an alias named $.[rx] All functions can be accessed through either of these two names.

jQuery methods

  • The jQuery the function is a factory for creating a jQuery object that represents one or more DOM nodes. jQuery objects have methods to manipulate these nodes. These methods (sometimes called commands) are chainable as each method also returns a jQuery object.
  • Access to and manipulation of multiple DOM nodes in jQuery typically begins with calling the function with a CSS selector string. This returns a jQuery object referencing all the matching elements in the HTML page. $("div.test"), for example, returns a jQuery object with all the div elements of class test. This node set can be manipulated by calling methods on the returned jQuery object.

Static utilities

  • These are utility functions and do not directly act upon a jQuery object. They are accessed as static methods on the jQuery or $ identifier. For example, $.ajax() is a static method.

No-conflict mode

  • jQuery provides a $.noConflict() function, which relinquishes control of the name. This is useful if jQuery is used on a Web page also linking another library that demands the $symbol as its identifier. In no-conflict mode, developers can use jQuery as a replacement for $ without losing functionality.[rx]

Typical start-point

  • Typically, jQuery is used by putting initialization code and event handling functions in $(handler). This is triggered by jQuery when the browser has finished constructing the DOM for the current Web page.
$(function () {
        // This anonymous function is called when the page has completed loading.
        // Here, one can place code to create jQuery objects, handle events, etc.
});

or

$(fn); // The function named fn, defined elsewhere, is called when the page has loaded.

Historically, $(document).ready(callback) has been the de facto idiom for running code after the DOM is ready. However, since jQuery 3.0, developers are encouraged to use the much shorter $(handler) signature instead.[rx]

Chaining

jQuery object methods typically also return a jQuery object, which enables the use of method chains:

$('div.test')
  .on('click', handleTestClick)
  .addClass('foo');

This line finds all div elements with a class attribute test , then registers an event handler on each element for the “click” event, then adds the class attribute foo to each element.

Certain jQuery object methods retrieve specific values (instead of modifying state). An example of this is the val() method, which returns the current value of a text input element. In these cases, a statement such as $('#user-email').val() cannot be used for chaining as the return value does not reference a jQuery object.

Creating new DOM elements

Besides accessing existing DOM nodes through jQuery, it is also possible to create new DOM nodes, if the string passed as the argument to $() factory looks like HTML. For example, the below code finds an HTML select element, and creates a new option element with value “VAG” and label “Volkswagen”, which is then appended to the select menu:

$('select#car-brands')
  .append($('<option>')
    .attr({ value: 'VAG' })
    .text('Volkswagen')
  );

Ajax

It is possible to make Ajax requests (with cross-browser support) with $.ajax() to load and manipulate remote data.

$.ajax({
  type: 'POST',
  url: '/process/submit.php',
  data: {
    name : 'John',
    location : 'Boston',
  },
}).then(function(msg) {
  alert('Data Saved: ' + msg);
}).catch(function(xmlHttpRequest, statusText, errorThrown) {
  alert(
    'Your form submission failed.nn'
      + 'XML Http Request: ' + JSON.stringify(xmlHttpRequest)
      + ',nStatus Text: ' + statusText
      + ',nError Thrown: ' + errorThrown);
});

This example posts the data name=John and location=Boston to /process/submit.php on the server. When this request finishes the success function is called to alert the user. If the request fails it will alert the user to the failure, the status of the request, and the specific error.

The above example uses the .then() and .catch() methods to register callbacks that run when the response has completed. These promise callbacks must be used due to the asynchronous nature of Ajax requests.

jQuery

 

jQuery Plug-ins

  • jQuery’s architecture allows developers to create plug-in code to extend its functionality. There are thousands of jQuery plug-ins available on the Web[rx] that cover a range of functions, such as Ajax helpers, Web services, data grids, dynamic lists, XML and XSLT tools, drag and drop, events, cookie handling, and modal windows.
  • An important source of jQuery plug-ins is the plugins sub-domain of the jQuery Project website.[rx] The plugins in this subdomain, however, were accidentally deleted in December 2011 in an attempt to rid the site of spam.[rx] The new site is a GitHub-hosted repository, which required developers to resubmit their plugins and to conform to new submission requirements.[rx]jQuery provides a “Learning Center” that can help users understand JavaScript and get started developing jQuery plugins.[rx]

References

jQuery

Print Friendly, PDF & Email

Sharing to Spread to the World

Leave a Reply

Your email address will not be published.