Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning jQuery, Third Edition

You're reading from   Learning jQuery, Third Edition Create better interaction, design, and web development with simple JavaScript techniques

Arrow left icon
Product type Paperback
Published in Sep 2011
Publisher Packt
ISBN-13 9781849516549
Length 428 pages
Edition 1st Edition
Tools
Arrow right icon
Toc

Table of Contents (24) Chapters Close

Learning jQuery Third Edition
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. Getting Started FREE CHAPTER 2. Selecting Elements 3. Handling Events 4. Styling and Animating 5. Manipulating the DOM 6. Sending Data with Ajax 7. Using Plugins 8. Developing Plugins 9. Advanced Selectors and Traversing 10. Advanced Events 11. Advanced Effects 12. Advanced DOM Manipulation 13. Advanced Ajax JavaScript Closures Testing JavaScript with QUnit Quick Reference Index

DOM traversal methods


After creating a jQuery object using $(), we can alter the set of matched elements we are working with by calling one of these DOM traversal methods. DOM traversal methods are discussed in detail in Chapter 2.

Filtering

Traversal Method

Returns a jQuery object containing

.filter(selector)

Selected elements that match the given selector

.filter(callback)

Selected elements for which the callback function returns true

.eq(index)

The selected element at the given 0-based index

.first()

The first selected element

.last()

The final selected element

.slice(start, [end])

Selected elements in the given range of 0-based indices

.not(selector)

Selected elements that do not match the given selector

.has(selector)

Selected elements that have a descendant matching selector

Descendants

Traversal Method

Returns a jQuery object containing

.find(selector)

Descendant elements that match the selector

.contents()

Child nodes (including text nodes)

.children([selector])

Child nodes, optionally filtered by a selector

Siblings

Traversal Method

Returns a jQuery object containing

.next([selector])

The sibling immediately following each selected element, optionally filtered by a selector

.nextAll([selector])

All siblings following each selected element, optionally filtered by a selector

.nextUntil([selector], [filter])

All siblings following each selected element, up to and not including the first element matching selector, optionally filtered by an additional selector

.prev([selector])

The sibling immediately preceding each selected element, optionally filtered by a selector

.prevAll([selector])

All siblings preceding each selected element, optionally filtered by a selector

.prevUntil([selector], [filter])

All siblings preceding each selected element, up to and not including the first element matching selector, optionally filtered by an additional selector

.siblings([selector])

All siblings, optionally filtered by a selector

Ancestors

Traversal Method

Returns a jQuery object containing

.parent([selector])

The parent of each selected element, optionally filtered by a selector

.parents([selector])

All ancestors, optionally filtered by a selector

.parentsUntil([selector], [filter])

All ancestors of each selected element, up to and not including the first element matching selector, optionally filtered by an additional selector

.closest(selector)

The first element that matches the selector, starting at the selected element and moving up through its ancestors in the DOM tree

.offsetParent()

The positioned parent (for example, relative, absolute) of the first selected element

Collection manipulation

Traversal Method

Returns a jQuery object containing

.add(selector)

Selected elements, plus any additional elements that match the given selector

.andSelf()

The selected elements, plus the previous set of selected elements on the internal jQuery stack

.end()

The previous set of selected elements on the internal jQuery stack

.map(callback)

The result of the callback function when called on each selected element

.pushStack(elements)

The specified elements

Working with selected elements

Traversal Method

Description

.is(selector)

Determine whether any matched element is matched by the given selector expression

.index()

Get the index of the matched element in relation to its siblings

.index(element)

Get the index of the given DOM node within the set of matched elements

$.contains(a, b)

Determine whether DOM node b contains DOM node a

.each(callback)

Iterate over the matched elements, executing callback for each element

.length

Get the number of matched elements

.get()

Get an array of DOM nodes corresponding to the matched elements

.get(index)

Get the DOM node corresponding to the matched element at the given index

.toArray()

Get an array of DOM nodes corresponding to the matched elements

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image