INTRODUCTION TO Jquery
INTRODUCTION TO Jquery
To build interesting and interactive sites, developers are turning to JavaScript libraries such as
jQuery to automate common tasks and to simplify complicated ones. One reason the jQuery library
is a popular
choice is its ability to assist in a wide range of tasks.
• Modify the appearance of a web page: CSS offers a powerful method of influencing the
way a document is rendered, but it falls short when not all web browsers support the same
standards. With jQuery, developers can
bridge this gap, relying on the same standards support across all browsers In addition, jQuery can
change the classes or individual style properties applied to a portion of the document even after the
page has been rendered.
$('ul > li:first').addClass('active');
• Alter the content of a document: Not limited to mere cosmetic changes, jQuery can
modify the content of a document itself with a few keystrokes. Text can be changed, images can be
inserted or swapped, lists can be
reordered, or the entire structure of the HTML can be rewritten and extended—all with a single
easy-to-use Application Programming Interface (API).
$('#container').append('<a href="more.html">more</a>');
• Respond to a user's interaction: Even the most elaborate and powerful behaviors are not
useful if we can't control when they take place. The jQuery library offers an elegant way to intercept
a wide variety of events, such as a user clicking on a link, without the need to clutter the HTML code
itself with
event handlers. At the same time, its event-handling API removes browser inconsistencies that often
plague web developers.
$('button.show-details').click(function() {
$('div.details').show();
});
• Animate changes being made to a document: To effectively implement such
interactive behaviors, a designer must also provide visual feedback to the user. The jQuery library
facilitates this by providing an array of effects such as fades and wipes, as well as a toolkit for
crafting new ones.
$('div.details').slideDown();
• Retrieve information from a server without refreshing a page: This code pattern
is known as Ajax, which originally stood for Asynchronous JavaScript and XML, but has
since come to represent a much greater set of technologies for communicating between the client
and the server. The jQuery library removes the browser- specific complexity from this responsive,
feature-rich process, allowing developers to focus on the server-end functionality.
$('div.details').load('more.html #content');
• Simplify common JavaScript tasks: In addition to all of the documentspecific features of
jQuery, the library provides enhancements to basic JavaScript constructs such as iteration and array
manipulation.
$.each(obj, function(key, value) {
total += value;
});
Save the file to your site. Like you’ve learned to organize your CSS in a separate folder, it can be
helpful to save your JavaScript in a separate folder as well. You can save it to a folder named js.
To start using jQuery, all you must do is include it in your page, like any other script :
<script src="jquery.js"></script>
Note: Unless it needs to be in the <head> for some reason (Modernizr is the most common
example of
a script that needs to be in the <head>), for top performance, JavaScript should be included right
before the closing </body> tag.
Selecting Elements:
Two of the most fundamental parts of the library: the jQuery or $ function and the $
(document ).ready() event.which are using to selecting the elements .
Introducing the $ Function
At the heart of every jQuery operation is the jQuery object. Everything you do with the library
starts there.
1. You can access this function with two different names: jQuery or as the more famous alias
$. In the context of programming with jQuery, they’re equivalent. The rest of the text
refers to $ exclusively.
2. $ is used in two different ways. Called as a function, $(), it enables you to access, create,
and manipulate HTML elements. $ also enables you to access additional methods and
properties of jQuery
3. In order to create a new jQuery object, we use the $() function. This function typically
accepts a CSS selector as its sole parameter and serves as a factory returning a new jQuery
object pointing to the corresponding elements on the page.
4. The three primary building blocks of selectors are tag name, ID, and class. They can be used
either on their own or in combination with others. The following simple examples illustrate
how these three selectors appear in code:
Start Your Scripts the Smart Way with $
( document ).ready()