Table of Contents
- Background
- Introduction
- Anatomy of jQuery
- Setting up jQuery ready handler
- A Quick Demo
- Digging deeper into jquery
- CSS Selectors
- Selecting by Element
- Selecting by ID
- Selecting by Class
- Selecting by Descendant (E1 E2)
- Selecting Child elements (E1 > E2)
- Adjacent Sibling (E1 + E2)
- General Sibling (E1 ~ E2)
- Multiple Elements (E1, E2, E3)
- Nth Child (:nth-child(n))
- First Child (:first-child)
- Last Child (:last-child)
- Only Child (:only-child)
- Not (:not(s))
- Empty (:empty)
- All Elements (*)
- XPath Selectors
- Attribute Selectors (*update)
- Form Selector
- Custom Selector
- DOM
- Playing with Events
- Effects and Animation
- Spicing up website with Ajax
- Generate Automatic TOC
- Limit text box entry
- Create your own Custom filter
- Plug with the Plug-In API
- Best Practices
- Building a poor man’s slide show app(*new)
- Building a treeview plugin(*new)
- References
- History
Background
Originally Published by me at : https://www.codeproject.com/Articles/39242/Getting-friendly-with-jQuery
This article is the summation of my notes collected during my attempt to learn jQuery through various books and websites. As such this is still a work in progress and will be updated based on my understanding and study of this beautiful library and will add simple uses cases of jquery with asp.net/mvc.
Introduction
jQuery is a javascript library with rich API to manipulate DOM, event handling, animation and ajax interactions. The following are the essential features of jQuery that makes it so appealing for client side scripting.
- jQuery is Cross-Browser
- jQuery supports Ajax
- Rich Selectors
- DOM Manipulation
- Animation
- Rich UI
Anatomy of jQuery
The jQuery architecture revolves around the following areas.
Setting up jQuery ready handler
jQuery lets you run your code when the page elements have been loaded. This is more efficient than the browser onload() functions, which is called only after all images have been loaded. To run the code when the page is ready you use the following syntaxHide Copy Code
$(document).ready(function() {
....
});
There's a shorthand as well
$(function() {
....
});
A Quick Demo
Let’s briefly have quick look at jQuery in action.
Selecting Page Elements by ID
The $(‘#id’) is used to select elements by id. When you click on the button “Stripe” the stripe() JS method is triggered which toggles the class of the element whose id matches ‘third’. On subsequent click the style will be reset.
The code and the markup is displayed below.Hide Shrink
Copy Code
Select a paragraph Select a paragraph
This is paragraph 1.
This is paragraph 2.
This is paragraph 3.
This is paragraph 4.
Selecting a Set of Elements
The $(“div”) selects all div elements. The fadeIn() effect is applied to each div matched.
The div is hidden initially.
On clicking the button the all the div elements fades in slowly.
The code and the markup is displayed below.Hide Copy Code
Selecting a set of Elements
Fade
Selecting Elements by Style
The $(p.mark) selects all paragraph elements with the class “mark”.

The code and the markup is displayed below.Hide Copy Code
Select a paragraph
Hide Copy Code
Select a paragraph
This is paragraph 1.
This is paragraph 2.
This is paragraph 3.
This is paragraph 4.
Digging deeper into jquery
The above discussion summed up the very basics of jQuery. The next sections dig deeper into jQuery based on the antomical structure outlined.

CSS Selectors
The CSS selectors are baed on the CSS 1-3 as outlined by the w3C. For additional information please visit w3.org
Selecting by Element
All elements selection takes the form of $(‘T’) where ‘T’ stands for the element tag name.
For e.g.
- $(‘div’) selects all elements with a tag name of div in the document
- $(‘input’) selects all elements with a tag name of input in the document.
jQuery uses the JavaScript’s getElementsByTagName() function for tag-name selectors.
Selecting by ID
This takes the form $(‘#id’) where ID equal to id.
For e.g.
- $(‘#firstName’) selects the unique element with id = ‘firstName’.
- $(‘div#main’) selects a single div with an id of ‘main’.
If there are more than one element with the same id then jQuery selects the first matched element in the DOM.
Selecting by Class
This takes the form $(‘.myclass’) where class equal to myclass.
For e.g.
- $(‘.blink’) selects all elements with class = ‘blink’.
- $(‘p.blink’) selects all paragraphs that have a class of blink.
- $(‘.mybold.myblink’) selects all elements that have a class of ‘mybold’ and ‘myblink’.
Performance wise the example 2 is preferable as it limits the query to a given tag name.
Selecting by Descendant (E1 E2)
Matches all elements matched by E2 that are descendants of an element matched by E1.
For e.g.
- $(‘#menu li’) selects all elements matched by
- that are descendants of an element that has an id of menu.
- $(‘a img’) selects all elements matched by
that are descendants of an element matched by
A descendant of an element is any element that is a child, grandchild and so on of that element.Hide Copy Code

