Jquery: 2 Features
Jquery: 2 Features
Jquery: 2 Features
This article is about the JavaScript library. For the actor, tion denition are done in a single step in a single location
see Jake Weary.
in the code. jQuery also aims to incorporate other highly
used JavaScript functionality (e.g. fade ins and fade outs
jQuery is a cross-platform JavaScript library designed to when hiding elements, animations by manipulating CSS
properties).
simplify the client-side scripting of HTML.[3] jQuery is
the most popular JavaScript library in use today, with in- The principles of developing with jQuery are:
stallation on 65% of the top 10 million highest-tracked
sites on the Web.[4][5][6] jQuery is free, open-source soft Separation of JavaScript and HTML: The
ware licensed under the MIT License.[2]
jQuery library provides simple syntax for adding
event handlers to the DOM using JavaScript,
rather than adding HTML event attributes to call
JavaScript functions. Thus, it encourages developers to completely separate JavaScript code from
HTML markup.
The set of jQuery core featuresDOM element selections, traversal and manipulationenabled by its selector engine (named Sizzle from v1.3), created a new
programming style, fusing algorithms and DOM data
structures. This style inuenced the architecture of other
JavaScript frameworks like YUI v3 and Dojo, later stimulating the creation of the standard Selectors API.[7]
2 Features
jQuery includes the following features:
Overview
USAGE
JSON parsing
a CSS selector string. This returns a jQuery object referencing all the matching elements in the HTML page.
Extensibility through plug-ins
$(div.test), for example, returns a jQuery object with
all the div elements of class test. This node set can be
Utilities, such as feature detection
manipulated by calling methods on the returned jQuery
Compatibility methods that are natively available in object or on the nodes themselves.
modern browsers, but need fall backs for older ones,
such as inArray() and each()
Usage
3.1
3.2
Usage styles
$(handler)
$().ready(handler)
$(document).ready(handler)
$(selector).ready(handler)
3.5 Chaining
3
This line nds the union of all div tags with class attribute
test and all p tags with class attribute quote, adds the
class attribute blue to each matched element, and then
increases their height with an animation. The $ and add
functions aect the matched set, while the addClass and
slideDown aect the referenced nodes.
Certain jQuery functions return specic values (such as
$('#input-user-email').val()). In these cases, chaining will
not work as the value does not reference the jQuery object.
3.8.1 Asynchronous
Note that the above example uses the deferred nature
of $.ajax() to handle its asynchronous nature: .done()
and .fail() create callbacks that run only when the asynchronous process is complete.
4 jQuery plug-ins
jQuerys architecture allows developers to create plugin code to extend its function. There are thousands of
3.6 Creating new DOM elements
jQuery plug-ins available on the Web[19] that cover a
range of functions, such as Ajax helpers, Web services,
Besides accessing DOM nodes through jQuery object hi- datagrids, dynamic lists, XML and XSLT tools, drag and
erarchy, it is also possible to create new DOM elements, drop, events, cookie handling, and modal windows.
if a string passed as the argument to $() looks like HTML. An important source of jQuery plug-ins is the plugFor example, this line nds an HTML select element ins subdomain of the jQuery Project website.[19] The
with ID carmakes, and adds an option element with value plugins in this subdomain, however, were accidentally
VAG and text Volkswagen":
deleted in December 2011 in an attempt to rid the
site of spam.[20] The new site will include a GitHubhosted repository, which will require developers to resubmit their plugins and to conform to new submission requirements.[21] There are alternative plug-in search
engines[22] like jquer.in that take more specialized approaches, such as listing only plug-ins that meet certain
3.7 Utility functions
criteria (e.g. those that have a public code repository).
Functions prexed with $. are utility functions or func- jQuery provides a Learning Center that can help users
JavaScript and get started developing jQuery
tions that aect global properties and behaviour. The fol- understand
[23]
plugins.
lowing, for example, is a function used for iterating over
arrays called each in jQuery:
$('select#carmakes).append($('<option
/>').attr({value:"VAG"}).append(Volkswagen));
3.8
Ajax
5 History
jQuery was originally released in January 2006 at
BarCamp NYC by John Resig and was inuenced by
Dean Edwards earlier cssQuery library.[24] It is currently
maintained by a team of developers led by Timmy Willison (with the jQuery selector engine, Sizzle, being led by
Richard Gibson).
It is possible to perform cross-browser Ajax requests using $.ajax. Its associated methods can be used to load and
jQuery has also an interesting software license history.[25]
manipulate remote data.
Originally under the CC BY-SA 2.5, it was relicensed to
$.ajax({ type: 'POST', url: '/process/submit.php', the MIT license in 2006.[26] On end of 2006 it was dualdata: { name : 'John', location : 'Boston', }, licensed under GPL and MIT license.[27] As this led to
}).done(function(msg) { alert('Data Saved: ' + some confusion, in 2012 the GPL was dropped and is
msg);
}).fail(function(xmlHttpRequest,
status- now only MIT licensed.[28]
Text, errorThrown) { alert( 'Your form submission failed.\n\n' + 'XML Http Request: ' + As of 2015, jQuery remains the most widely used
JSON.stringify(xmlHttpRequest) + ',\nStatus Text: JavaScript library on the Web.
' + statusText + ',\nError Thrown: ' + errorThrown); });
This example posts the data name=John and location=Boston to /process/submit.php on the server. When
this request nishes the success function is called to alert 6 Testing framework
the user. If the request fails it will alert the user to the
QUnit is a test automation framework used to test the
failure, the status of the request, and the specic error.
10
EXTERNAL LINKS
jQuery project. The jQuery team developed it as an [16] Microsoft Ajax Content Delivery Network. ASP.net.
Microsoft Corporation. Retrieved June 19, 2012.
in-house unit testing library.[29] The jQuery team uses
it to test its code and plugins, but it can test any
[17] jQuery.noConict() jQuery API Documentation.
generic JavaScript code, including server-side JavaScript
code.[29]
[18] $(document).ready deprecated
As of 2011, the jQuery Testing Team uses QUnit with
TestSwarm to test each jQuery codebase release.[30]
[20] What Is Happening To The jQuery Plugins Site?". Retrieved 22 April 2015.
See also
jQuery Mobile
jQuery UI
Globalize
[24] York, Richard (2009). Beginning JavaScript and CSS Development with jQuery. Wiley. p. 28. ISBN 978-0-47022779-4.
References
10 External links
Dmitri Gaskin Google Tech Talk, 2008 on YouTube
Ocial website
9 Further reading
Guide.
11
11.1
11.2
Images
11.3
Content license