SlideShare a Scribd company logo
www.webstackacademy.com
DOM Handling
jQuery
www.webstackacademy.comwww.webstackacademy.com
DOM handling - jQuery
(Write less, Do more)
www.webstackacademy.com
Document Object Model (DOM)
• The W3C Document Object Model (DOM) is a platform and language-neutral interface
that allows programs and scripts to dynamically access and update the content,
structure, and style of a document
• jQuery provides multiple methods to manipulate DOM
• Using these methods it is easy to access and manipulate elements and attributes
• It also provides methods to change browser window dimensions
www.webstackacademy.com
DOM APIs
Selector Description
$(selector).text(); Sets or returns the text content of selected elements
$(selector).html(); Sets or returns the content of selected elements (including HTML mark-up)
$(selector).val(); Sets or returns the value of form fields
www.webstackacademy.com
Example usage – Getting values using DOM APIs
<script>
$(document).ready(function() {
/* Get values from various HTML elements */
$("#btn1").click(function(){
console.log("Paragraph" + $("#test1").text());
console.log("Value" + $("#test2").val());
});
});
</script>
www.webstackacademy.com
Example usage – Setting values using DOM APIs
<script>
$(document).ready(function() {
/* Upon button click change text */
$("#btn1").click(function(){
$("#test1").text("First paragraph - normal text");
});
/* Upon button click change HTML text */
$("#btn2").click(function(){
$("#test2").html("<b>Second paragraph</b>");
});
/* Upon button click change input field value */
$("#btn3").click(function(){
$("#test3").val("New Value...");
});
});
</script>
www.webstackacademy.com
DOM APIs – Insert and Remove
Selector Description
$(selector).append(); Inserts content at the end of the selected elements
$(selector).prepend(); Inserts content at the beginning of the selected elements
$(selector).after(); Inserts content after the selected elements
$(selector).before() Inserts content before the selected elements
$(selector).remove(); Removes the selected element (and its child elements)
$(selector).empty(); Removes the child elements from the selected element
www.webstackacademy.com
Example usage – Insert and remove using DOM APIs
<script>
$(document).ready(function() {
$("#myButton2").click(function(){
$("#test2").remove();
});
$("#myButton4").click(function(){
$("#test3").append(" <li><b>Appended text</b></li> ");
});
});
</script>
www.webstackacademy.com
DOM APIs – Handling styles
Selector & Description
$(selector).css("property", "value");
$(selector).css({"property": "value", "property": "value",....});
Set a single or multiple properties on a selector
$("p").css("background-color", "blue");
$("p").css({"background-color": "red", "font-size": "100%"});
www.webstackacademy.comwww.webstackacademy.com
Dimension APIs
(Write less, Do more)
www.webstackacademy.com
jQuery – Dimension APIs
Selector Description
$(selector).width(); Sets or returns the width of an element (excludes padding, border and
margin)
$(selector).height(); Sets or returns the height of an element (excludes padding, border and
margin).
$(selector).innerWidth(); Returns the width of an element (includes padding)
$(selector).innerHeight(); Returns the height of an element (includes padding)
$(selector).outerWidth(); Returns the width of an element (includes padding and border)
$(selector).outerHeight(); Returns the height of an element (includes padding and border)
www.webstackacademy.com
jQuery – Dimension APIs
www.webstackacademy.com
Example usage – Dimensions
<script>
$(document).ready(function() {
$("button").click(function(){
console.log("Width: " + $("#div1").width());
console.log("Height: " + $("#div1").height());
console.log("OuterWidth: " + $("#div1").outerWidth());
console.log("OuterHeight: " + $("#div1").outerHeight());
});
});
</script>
www.webstackacademy.comwww.webstackacademy.com
DOM Traversal - jQuery
(Write less, Do more)
www.webstackacademy.com
What is DOM traversal?
• DOM is organized in a tree structure where each node has parent and child nodes
• Typically leaf node contains the resource (ex: Text / Image etc..)
• jQuery offers multiple APIs to "traverse" (walk through) DOM tree by addressing using
relationships
• U can find / select single or multiple DOM nodes based on the selector tag provided to
jQuery
• Similar to other features (ex: effects), using jQuery DOM traversal can be done easily
www.webstackacademy.com
DOM Tree - Terminology
• To understand the tree structure and manipulate using jQuery few terminology need to
be understood with respect to a particular node
o Move up to find Ancestors
o Move down to find Descendants
o Move sideways to find Siblings
Element Relationship information
<div> Ancestor of all
<li> Parent of <span> child of <ul> and
descendant of <div>
<li><li> Siblings
www.webstackacademy.com
jQuery APIs - Traversing
Selector Description
$(selector).parent(); It returns the direct parent element of the selected element. By passing
an optional parameters it will return parents of that type (ex:
parent("ul"))
$(selector).parents(); It returns all ancestor elements of the selected element, all the way up to
the document's root element (<html>)
$(selector).parentsUntil(); The parentsUntil() method returns all ancestor elements between two
given arguments
Parents –
Traverse upwards
www.webstackacademy.com
Example usage – Parents Traversing
<script>
$(document).ready(function() {
/* Find parent of span element and change its attributes */
$("span").parent().css({"color": "red", "border": "2px solid red"});
/* Find all parents of span element all the way up-to HTML root */
$("span").parents().css({"color": "blue", "border": "2px solid blue"});
/* Filter parents that are only UL type */
$("span").parents("ul").css({"color": "green", "border": "2px solid green"});
/* Parent elements till DIV type */
$("span").parentsUntil("div").css({"color": "purple", "border": "2px solid
purple"});
});
</script>
www.webstackacademy.com
jQuery APIs - Descendants Traversing
Selector Description
$(selector).children(); It returns all direct children of the selected element. By passing an
optional parameter it will return children of particular type (ex:
children("p.first"))
$(selector).find(); It returns descendant elements of the selected element, all the way
down to the last descendant. By passing an optional parameter it will
return descendant of particular type (ex: find("span"))
Descendants –
Traverse downwards
www.webstackacademy.com
Example usage – Descendants Traversing
<script>
$(document).ready(function() {
/* Find children of div element and change its attributes */
$("div").children().css({"color": "red", "border": "2px solid red"});
/* Find children which are of particular class */
$("div").children("p.first").css({"color": "green", "border": "2px solid
green"});
/* Find a specific child */
$("div").find("span").css({"color": "purple", "border": "2px solid purple"});
/* Find all children */
$("div").find("*").css({"color": "pink", "border": "2px solid pink"});
});
</script>
www.webstackacademy.com
jQuery APIs - Siblings Traversing
Selector Description
$(selector).siblings(); It returns all sibling elements of the selected element
$(selector).next(); It returns the next sibling element of the selected element
$(selector).nextAll(); It returns all next sibling elements of the selected element
$(selector).nextUntil(); It returns all next sibling elements between two given arguments
$(selector).prev(); It returns the previous sibling element of the selected element
$(selector).prevAll(); It returns all next previous elements of the selected element
$(selector).prevUntil(); It returns all previous sibling elements between two given arguments
Siblings – Traverse
sideways
www.webstackacademy.com
Example usage – Siblings Traversing
<script>
$(document).ready(function() {
/* Find all siblings */
$("h2").siblings().css({"color": "red", "border": "2px solid red"});
/* Find children which are of particular type */
$("h2").siblings("p").css({"color": "green", "border": "2px solid green"});
/* Find the next sibling */
$("h2").next().css({"color": "purple", "border": "2px solid purple"});
});
</script>
www.webstackacademy.com
jQuery APIs - Filtering
Selector Description
$(selector).first(); It returns the first element of the specified elements
$(selector).last(); It returns the last element of the specified elements
$(selector).eq(); It returns an element with a specific index number of the selected elements
$(selector).filter(); It lets you specify a criteria. Elements that do not match the criteria are removed
from the selection, and those that match will be returned.
$(selector).not(); It returns all elements that do not match the criteria
www.webstackacademy.com
Example usage – Filtering
<script>
$(document).ready(function() {
/* Select first div type element and its children */
$("div").first().css("background-color", "yellow");
/* Select last div type element and its children */
$("div").last().css("background-color", "green");
});
</script>
www.webstackacademy.com
WebStack Academy
#83, Farah Towers,
1st Floor, MG Road,
Bangalore – 560001
M: +91-809 555 7332
E: training@webstackacademy.com
WSA in Social Media:

More Related Content

What's hot (20)

PPT
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
PDF
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
Chris Ohk
 
PPTX
ReactJS presentation.pptx
DivyanshGupta922023
 
PPTX
Introduction to spring boot
Santosh Kumar Kar
 
PPTX
Clean code
Henrique Smoco
 
PPTX
Validation controls in asp
Shishir Jain
 
PDF
Vue入門
Takeo Noda
 
PPT
Hibernate
VISHAL DONGA
 
PDF
TypeScript Best Practices
felixbillon
 
PDF
Three.js basics
Vasilika Klimova
 
PPTX
React hooks
Sadhna Rana
 
PDF
React new features and intro to Hooks
Soluto
 
PPT
JavaScript & Dom Manipulation
Mohammed Arif
 
PPTX
Intro to Flexbox - A Magical CSS Property
Adam Soucie
 
PPT
Java multi threading
Raja Sekhar
 
PPTX
Spring Boot and REST API
07.pallav
 
PDF
Introducing CSS Grid Layout
Rachel Andrew
 
PDF
Introduction to A-Frame
Daosheng Mu
 
PPT
JQuery introduction
NexThoughts Technologies
 
PPTX
React state
Ducat
 
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
인프콘 2022 - Rust 크로스 플랫폼 프로그래밍
Chris Ohk
 
ReactJS presentation.pptx
DivyanshGupta922023
 
Introduction to spring boot
Santosh Kumar Kar
 
Clean code
Henrique Smoco
 
Validation controls in asp
Shishir Jain
 
Vue入門
Takeo Noda
 
Hibernate
VISHAL DONGA
 
TypeScript Best Practices
felixbillon
 
Three.js basics
Vasilika Klimova
 
React hooks
Sadhna Rana
 
React new features and intro to Hooks
Soluto
 
JavaScript & Dom Manipulation
Mohammed Arif
 
Intro to Flexbox - A Magical CSS Property
Adam Soucie
 
Java multi threading
Raja Sekhar
 
Spring Boot and REST API
07.pallav
 
Introducing CSS Grid Layout
Rachel Andrew
 
Introduction to A-Frame
Daosheng Mu
 
JQuery introduction
NexThoughts Technologies
 
React state
Ducat
 

Similar to jQuery - Chapter 4 - DOM Handling (20)

PDF
Introduction to jQuery
Zeeshan Khan
 
PPTX
Iniciando com jquery
Danilo Sousa
 
PPTX
Getting Started with jQuery
Akshay Mathur
 
PPT
J query lecture 1
Waseem Lodhi
 
PPTX
Web technologies-course 11.pptx
Stefan Oprea
 
PPTX
UNIT 1 (7).pptx
DrDhivyaaCRAssistant
 
PPTX
FFW Gabrovo PMG - jQuery
Toni Kolev
 
PDF
jQuery for beginners
Siva Arunachalam
 
PDF
jQuery
Ivano Malavolta
 
PPTX
J111111111111111111111111111111111111111query.pptx
dkmishra2407
 
PPT
Don't Worry jQuery is very Easy:Learning Tips For jQuery
shabab shihan
 
PPT
jQuery
Niladri Karmakar
 
PDF
JavaScript: DOM and jQuery
維佋 唐
 
PDF
jQuery Features to Avoid
dmethvin
 
PPTX
jQuery
Julie Iskander
 
PPTX
jQuery Presentasion
Mohammad Usman
 
PPT
J query introduction
SMS_VietNam
 
PDF
fuser interface-development-using-jquery
Kostas Mavridis
 
PPTX
Jquery fundamentals
Salvatore Fazio
 
PPTX
J query1
Manav Prasad
 
Introduction to jQuery
Zeeshan Khan
 
Iniciando com jquery
Danilo Sousa
 
Getting Started with jQuery
Akshay Mathur
 
J query lecture 1
Waseem Lodhi
 
Web technologies-course 11.pptx
Stefan Oprea
 
UNIT 1 (7).pptx
DrDhivyaaCRAssistant
 
FFW Gabrovo PMG - jQuery
Toni Kolev
 
jQuery for beginners
Siva Arunachalam
 
J111111111111111111111111111111111111111query.pptx
dkmishra2407
 
Don't Worry jQuery is very Easy:Learning Tips For jQuery
shabab shihan
 
JavaScript: DOM and jQuery
維佋 唐
 
jQuery Features to Avoid
dmethvin
 
jQuery Presentasion
Mohammad Usman
 
J query introduction
SMS_VietNam
 
fuser interface-development-using-jquery
Kostas Mavridis
 
Jquery fundamentals
Salvatore Fazio
 
J query1
Manav Prasad
 
Ad

More from WebStackAcademy (20)

PDF
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
PDF
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
PDF
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
PDF
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
PDF
Webstack Academy - Internship Kick Off
WebStackAcademy
 
PDF
Building Your Online Portfolio
WebStackAcademy
 
PDF
Front-End Developer's Career Roadmap
WebStackAcademy
 
PDF
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PDF
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
PDF
Angular - Chapter 5 - Directives
WebStackAcademy
 
PDF
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
PDF
Angular - Chapter 3 - Components
WebStackAcademy
 
PDF
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
PDF
Angular - Chapter 1 - Introduction
WebStackAcademy
 
PDF
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
PDF
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
PDF
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
PDF
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
Webstack Academy - Internship Kick Off
WebStackAcademy
 
Building Your Online Portfolio
WebStackAcademy
 
Front-End Developer's Career Roadmap
WebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
Angular - Chapter 5 - Directives
WebStackAcademy
 
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Angular - Chapter 3 - Components
WebStackAcademy
 
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
Angular - Chapter 1 - Introduction
WebStackAcademy
 
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Ad

Recently uploaded (20)

PDF
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
PPTX
CapCut Pro PC Crack Latest Version Free Free
josanj305
 
PDF
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PPTX
Wondershare Filmora Crack Free Download 2025
josanj305
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
PDF
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
PDF
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PDF
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
PDF
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
PDF
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
PPTX
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
PPTX
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Quantum Threats Are Closer Than You Think – Act Now to Stay Secure
WSO2
 
CapCut Pro PC Crack Latest Version Free Free
josanj305
 
Understanding AI Optimization AIO, LLMO, and GEO
CoDigital
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
Wondershare Filmora Crack Free Download 2025
josanj305
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Pipeline Industry IoT - Real Time Data Monitoring
Safe Software
 
Darley - FIRST Copenhagen Lightning Talk (2025-06-26) Epochalypse 2038 - Time...
treyka
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
Enhancing Environmental Monitoring with Real-Time Data Integration: Leveragin...
Safe Software
 
“A Re-imagination of Embedded Vision System Design,” a Presentation from Imag...
Edge AI and Vision Alliance
 
Dev Dives: Accelerating agentic automation with Autopilot for Everyone
UiPathCommunity
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
2025 HackRedCon Cyber Career Paths.pptx Scott Stanton
Scott Stanton
 
Kubernetes - Architecture & Components.pdf
geethak285
 
''Taming Explosive Growth: Building Resilience in a Hyper-Scaled Financial Pl...
Fwdays
 
Smarter Governance with AI: What Every Board Needs to Know
OnBoard
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 

jQuery - Chapter 4 - DOM Handling

  • 3. www.webstackacademy.com Document Object Model (DOM) • The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document • jQuery provides multiple methods to manipulate DOM • Using these methods it is easy to access and manipulate elements and attributes • It also provides methods to change browser window dimensions
  • 4. www.webstackacademy.com DOM APIs Selector Description $(selector).text(); Sets or returns the text content of selected elements $(selector).html(); Sets or returns the content of selected elements (including HTML mark-up) $(selector).val(); Sets or returns the value of form fields
  • 5. www.webstackacademy.com Example usage – Getting values using DOM APIs <script> $(document).ready(function() { /* Get values from various HTML elements */ $("#btn1").click(function(){ console.log("Paragraph" + $("#test1").text()); console.log("Value" + $("#test2").val()); }); }); </script>
  • 6. www.webstackacademy.com Example usage – Setting values using DOM APIs <script> $(document).ready(function() { /* Upon button click change text */ $("#btn1").click(function(){ $("#test1").text("First paragraph - normal text"); }); /* Upon button click change HTML text */ $("#btn2").click(function(){ $("#test2").html("<b>Second paragraph</b>"); }); /* Upon button click change input field value */ $("#btn3").click(function(){ $("#test3").val("New Value..."); }); }); </script>
  • 7. www.webstackacademy.com DOM APIs – Insert and Remove Selector Description $(selector).append(); Inserts content at the end of the selected elements $(selector).prepend(); Inserts content at the beginning of the selected elements $(selector).after(); Inserts content after the selected elements $(selector).before() Inserts content before the selected elements $(selector).remove(); Removes the selected element (and its child elements) $(selector).empty(); Removes the child elements from the selected element
  • 8. www.webstackacademy.com Example usage – Insert and remove using DOM APIs <script> $(document).ready(function() { $("#myButton2").click(function(){ $("#test2").remove(); }); $("#myButton4").click(function(){ $("#test3").append(" <li><b>Appended text</b></li> "); }); }); </script>
  • 9. www.webstackacademy.com DOM APIs – Handling styles Selector & Description $(selector).css("property", "value"); $(selector).css({"property": "value", "property": "value",....}); Set a single or multiple properties on a selector $("p").css("background-color", "blue"); $("p").css({"background-color": "red", "font-size": "100%"});
  • 11. www.webstackacademy.com jQuery – Dimension APIs Selector Description $(selector).width(); Sets or returns the width of an element (excludes padding, border and margin) $(selector).height(); Sets or returns the height of an element (excludes padding, border and margin). $(selector).innerWidth(); Returns the width of an element (includes padding) $(selector).innerHeight(); Returns the height of an element (includes padding) $(selector).outerWidth(); Returns the width of an element (includes padding and border) $(selector).outerHeight(); Returns the height of an element (includes padding and border)
  • 13. www.webstackacademy.com Example usage – Dimensions <script> $(document).ready(function() { $("button").click(function(){ console.log("Width: " + $("#div1").width()); console.log("Height: " + $("#div1").height()); console.log("OuterWidth: " + $("#div1").outerWidth()); console.log("OuterHeight: " + $("#div1").outerHeight()); }); }); </script>
  • 15. www.webstackacademy.com What is DOM traversal? • DOM is organized in a tree structure where each node has parent and child nodes • Typically leaf node contains the resource (ex: Text / Image etc..) • jQuery offers multiple APIs to "traverse" (walk through) DOM tree by addressing using relationships • U can find / select single or multiple DOM nodes based on the selector tag provided to jQuery • Similar to other features (ex: effects), using jQuery DOM traversal can be done easily
  • 16. www.webstackacademy.com DOM Tree - Terminology • To understand the tree structure and manipulate using jQuery few terminology need to be understood with respect to a particular node o Move up to find Ancestors o Move down to find Descendants o Move sideways to find Siblings Element Relationship information <div> Ancestor of all <li> Parent of <span> child of <ul> and descendant of <div> <li><li> Siblings
  • 17. www.webstackacademy.com jQuery APIs - Traversing Selector Description $(selector).parent(); It returns the direct parent element of the selected element. By passing an optional parameters it will return parents of that type (ex: parent("ul")) $(selector).parents(); It returns all ancestor elements of the selected element, all the way up to the document's root element (<html>) $(selector).parentsUntil(); The parentsUntil() method returns all ancestor elements between two given arguments Parents – Traverse upwards
  • 18. www.webstackacademy.com Example usage – Parents Traversing <script> $(document).ready(function() { /* Find parent of span element and change its attributes */ $("span").parent().css({"color": "red", "border": "2px solid red"}); /* Find all parents of span element all the way up-to HTML root */ $("span").parents().css({"color": "blue", "border": "2px solid blue"}); /* Filter parents that are only UL type */ $("span").parents("ul").css({"color": "green", "border": "2px solid green"}); /* Parent elements till DIV type */ $("span").parentsUntil("div").css({"color": "purple", "border": "2px solid purple"}); }); </script>
  • 19. www.webstackacademy.com jQuery APIs - Descendants Traversing Selector Description $(selector).children(); It returns all direct children of the selected element. By passing an optional parameter it will return children of particular type (ex: children("p.first")) $(selector).find(); It returns descendant elements of the selected element, all the way down to the last descendant. By passing an optional parameter it will return descendant of particular type (ex: find("span")) Descendants – Traverse downwards
  • 20. www.webstackacademy.com Example usage – Descendants Traversing <script> $(document).ready(function() { /* Find children of div element and change its attributes */ $("div").children().css({"color": "red", "border": "2px solid red"}); /* Find children which are of particular class */ $("div").children("p.first").css({"color": "green", "border": "2px solid green"}); /* Find a specific child */ $("div").find("span").css({"color": "purple", "border": "2px solid purple"}); /* Find all children */ $("div").find("*").css({"color": "pink", "border": "2px solid pink"}); }); </script>
  • 21. www.webstackacademy.com jQuery APIs - Siblings Traversing Selector Description $(selector).siblings(); It returns all sibling elements of the selected element $(selector).next(); It returns the next sibling element of the selected element $(selector).nextAll(); It returns all next sibling elements of the selected element $(selector).nextUntil(); It returns all next sibling elements between two given arguments $(selector).prev(); It returns the previous sibling element of the selected element $(selector).prevAll(); It returns all next previous elements of the selected element $(selector).prevUntil(); It returns all previous sibling elements between two given arguments Siblings – Traverse sideways
  • 22. www.webstackacademy.com Example usage – Siblings Traversing <script> $(document).ready(function() { /* Find all siblings */ $("h2").siblings().css({"color": "red", "border": "2px solid red"}); /* Find children which are of particular type */ $("h2").siblings("p").css({"color": "green", "border": "2px solid green"}); /* Find the next sibling */ $("h2").next().css({"color": "purple", "border": "2px solid purple"}); }); </script>
  • 23. www.webstackacademy.com jQuery APIs - Filtering Selector Description $(selector).first(); It returns the first element of the specified elements $(selector).last(); It returns the last element of the specified elements $(selector).eq(); It returns an element with a specific index number of the selected elements $(selector).filter(); It lets you specify a criteria. Elements that do not match the criteria are removed from the selection, and those that match will be returned. $(selector).not(); It returns all elements that do not match the criteria
  • 24. www.webstackacademy.com Example usage – Filtering <script> $(document).ready(function() { /* Select first div type element and its children */ $("div").first().css("background-color", "yellow"); /* Select last div type element and its children */ $("div").last().css("background-color", "green"); }); </script>
  • 25. www.webstackacademy.com WebStack Academy #83, Farah Towers, 1st Floor, MG Road, Bangalore – 560001 M: +91-809 555 7332 E: [email protected] WSA in Social Media: