Learn jquery from beginners to advanced level - part 2

In this programming tutorial we will learn jquery from beginners to advanced level. If you are new visitor and haven't read my first tutorial of this series then first please read the tutorial Learn jquery from beginners to advanced level - part 1. Ok. Now let's start right from there, where we were.
Selectors demo

Document Traversal

A Selector returns a pseudo array of jQuery objects

Returns number of selected elements. It is the best way to check selector.

Getting a specific DOM element

 $(“div”).get(2) or $(“div”)[2]

Returns a 2nd DOM element of the selection

Getting a specific jQuery element


Returns a 2nd jQuery element of the selection

each(fn) traverses every selected element calling fn()
var sum = 0;
		  sum += (+this.innerHTML);

this – is a current DOM element

each(fn) also passes an indexer

$(“table tr”).each(
		if (i % 2) 

$(this) – convert DOM to jQuery i - index of the current element

Traversing HTML
.next(expr) 	// next sibling

.prev(expr)		// previous sibling

.siblings(expr)	// siblings

.children(expr) // children

.parent(expr) 	// parent

Check for expression
$(“table td”).each(function() {
	if ($(this).is(“:first-child”)) {

Find in selected
// select paragraph and then find 
// elements with class ‘header’ inside 

 // equivalent to:
$(“.header”, $(“p”)).show();

Advanced Chaining
$(“<li><span></span></li>”) // li
	.find(“span”) // span
		.html(“About Us”) // span 
		.andSelf() // span, li
			.addClass(“menu”) // span,li 
		.end() // span 
	.end() // li 

Get Part of Selected Result
$(“div”).slice(2, 5).not(“.green”).addClass(“middle”);

HTML Manipulation

Getting and Setting Inner Content
$(“p”).html(“<div>Hello World!</div>”);

// escape the content of div.b

Getting and Setting Values
// get the value of the checked checkbox

// set the value of the textbox

// select or check lists or checkboxes

Handling CSS Classes
// add and remove class

// add if absent, remove otherwise

// test for class existance
 if ($(“div”).hasClass(“main”)) { 
// Your code will come here


Inserting new Elements
// select > append to the end
$(“h1”).append(“<li>Hello World</li>”);

// select > append to the beginning
$(“ul”).prepend(“<li>Hello World</li>”);

// create > append/prepend to selector



Replacing Elements
// select > replace

// create > replace selection

Replacing Elements while keeping the content
	$(this).replaceWith(“<div>” + $(this).html() 
					+ ”</div>”);

Deleting Elements
// remove all children

// remove selection

// change position

Handling attributes
// <a href=“home.htm”>…</a>

// set the same as the first one
// remove attribute - enable

Setting multiple attributes
		“src” : “/images/smile.jpg”,
		“alt” : “Smile”,
		“width”  : 10,
		“height” : 10

CSS Manipulations
// get style

// set style 
$(“div”).css(“float”, “left”);

// set multiple style properties
			 “padding”: “1em”
			 “margin-right”: “0”,
			 marginLeft: “10px”});

// get window height
var winHeight = $(window).height();

// set element height

//.width() – element
//.innerWidth() – .width() + padding

//.outerWidth() – .innerWidth() + border
//.outerWidth(true) – including margin

The default unit is Pixel (px)

// from the document

// from the parent element

// scrolling position

So that's it, i try my level best to let you learn jquery from beginners to advanced level with small and handy examples. Third and Final part of this tutorial series will come soon. Stay tuned.

Your feedback always provides me lot of energy.