Professional Documents
Culture Documents
C3: Protected
Confidential
Icons Used
Hands on Exercise
Questions
Tools
Coding Standards
Reference
Demonstration
A Welcome Break
Contacts
Confidential
conditions.
We can traverse the DOM using the following three building blocks while
Confidential
jQuery : Objectives
Objective:
After completing this chapter you will be able to: Define Traversing. Describe the functions available in Traversing .
Confidential
jQuery - Traversing
jQuery provides various traversal methods to select elements randomly or in
sequential method. eq(index) method - Index starts its count from 0. Example: $("li").eq(2) <ul> <li>list item 1</li> <li>list item 2</li> </ul>
Confidential
Example: $("li").filter(".middle").addClass("selected") <style> .selected { color:red; } </style> <ul> <li class="top">list item 1</li> <li class="middle">list item 3</li> <li class="bottom">list item 5</li> </ul>
Confidential
type of elements
Example: $("p").find("span").addClass("selected")
<style> .selected { color:red; } </style> <p>This is 1st paragraph and <span>THIS IS RED</span></p> <p>This is 2nd paragraph and <span>THIS IS ALSO RED</span></p>
Confidential
Example: $("li").filter(".middle").addClass("selected") <style> .selected { color:red; } </style> <ul> <li class="top">list item 1</li> <li class="middle">list item 3</li> <li class="bottom">list item 5</li>
</ul>
Confidential
and returns true or false element.is( selector ) Example: $("li").click(function () { if ($(this).is(":first-child")) { $("p").text("This is list item 1"); } else if ($(this).is(".middle0,.middle1")) { $("p").text("This is middle class list"); } else if ($(this).is(":contains('item 5')")) { $("p").text("It's 5th list"); } });
Confidential
10
Confidential
11
callback: The function to execute on each element in the set selector.map( callback ) Example: var mappedItems = $("li").map(function (index) { var replacement = $("<li>").text($(this).text()).get(0); if (index == 0) { $(replacement).text($(replacement).text().toUpperCase()); } else if (index == 1 || index == 3) { replacement = null; } else if (index == 2) { $(replacement[0]).append("<b> - A</b>"); $(replacement[1]).append("Extra <b> - B</b>"); } return replacement; }); $("#results").append(mappedItems);
2007, Cognizant Technology Solutions Confidential 12
selector from the set of matched elements Selector can be multiple to apply multiple filters selector.not( selector )
Confidential
13
Confidential
15
Confidential
16
jQuery
Confidential
18
jQuery
Questions from participants
Confidential
19
Confidential
20
jQuery :Summary
From this session, we have seen various methods
To traverse DOM elements up or down in random or sequential method To filter the DOM elements based on some condition To select subset of the DOM elements
Confidential
21
http://www.w3schools.com/jquery/default.asp
http://weblogs.asp.net/scottgu/archive/2008/09/28/jquery-
and-microsoft.aspx
Disclaimer: Parts of the content of this course is based on the materials available from the Web sites and books listed above. The materials that can be accessed from linked sites are not maintained by Cognizant Academy and we are not responsible for the contents thereof. All trademarks, service marks, and trade names in this course are the marks of the respective owner(s).
2007, Cognizant Technology Solutions Confidential 22