Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
jQuery 2.0 Development Cookbook

You're reading from   jQuery 2.0 Development Cookbook As a web developer, you can benefit greatly from this book - whatever your skill level. Learn how to build dynamic modern websites using jQuery. Packed with recipes, it will quickly take you from beginner to expert.

Arrow left icon
Product type Paperback
Published in Feb 2014
Publisher Packt
ISBN-13 9781783280896
Length 410 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Leon Revill Leon Revill
Author Profile Icon Leon Revill
Leon Revill
Arrow right icon
View More author details
Toc

Table of Contents (12) Chapters Close

Preface 1. Document Object Model Manipulation FREE CHAPTER 2. Interacting with the User by Making Use of jQuery Events 3. Loading and Manipulating Dynamic Content with AJAX and JSON 4. Adding Attractive Visuals with jQuery Effects 5. Form Handling 6. User Interface 7. User Interface Animation 8. Understanding Plugin Development 9. jQuery UI 10. Working with jQuery Mobile Index

Finding and selecting sibling elements

You may not always know the specific element that you need to select. You may only know its parent, and therefore, you will need to search through the elements within the parent in order to find the specific element that you are looking for. This recipe will show you how to find elements through their parents in various ways.

Getting ready

Open your text editor or IDE with the latest version of jQuery, ready to be included into the HTML page that you will create as part of this recipe.

How to do it…

To learn the various ways in which jQuery can help you to search for DOM elements based on a parent element, perform each of the following steps:

  1. Create a web page with the following HTML and JavaScript code:
    <!DOCTYPE html>
    <html>
    <head>
       <title>Finding and selecting sibling elements</title>
       <script src="jquery.min.js"></script>
       <script>
          $(function(){
             var element1 = $('#content .top .top-left'); //Select the top left division element
             var element2 = $('.parent').find('a'); //Select the anchor element
             var element3 = $('.parent').find('.grandchild'); //Select the grandchild element
          });
       </script>
    </head>
    <body>
    <div class="division-container">Some text <span>within</span> a div <span>which</span> has a many <span>span</span> elements.</div>
    <div id="content">
       <div class="top">
          <div class="top-left">Left</div>
          <div class="top-right">Right</div>
       </div>
    </div>
    <ul class="info-list">
       <li>List Item 1</li>
       <li>List Item 2</li>
       <li>List Item 3</li>
    </ul>
    <ul class="second-info-list">
       <li>Second List Item 1</li>
       <li>Second List Item 2</li>
       <li>Second List Item 3</li>
    </ul>
    <div class="parent">
       <div class="child">
          <div class="grandchild">
             <a href="#">A Link</a>
          </div>
       </div>
    </div>
    </body>
    </html>
  2. This code uses multiple class names in the same way as you would use them with CSS to select child elements from HTML. Alternatively, you can use jQuery's find() function on a parent element to search within.

How it works…

The simplest way to select a child element based on its parent is by using the same selectors as you would in CSS (that is, .classname .anotherclass). Having said this, you do not always know the exact location of the sibling element you are looking for. If this is the case, we can use the useful jQuery's find() function. jQuery's find() function will search within the specified parent element for the sibling element that you are looking for.

Based on the HTML within the How to do it… section, the following JavaScript illustrates how you can access a child element directly in the same manner as you would in CSS:

$(function(){
   var element1 = $('#content .top .top-left');
});

This would make the DOM element available within the content variable. More on what this means is covered later in the chapter.

To find a child element without knowing its exact location, we can use the following JavaScript to locate the anchor within the <div class="grandchild"> element:

$(function(){
   var element2 = $('.parent').find('a');
});

Note that you only need to specify the parent selector and the element you are looking for. The find() method simply traverses the DOM based on the specified parent element until it either finds the element you are looking for or runs out of elements to check against. You can use ID and class names within the find() method as well as HTML notations.

There's more…

You can also use CSS3 selectors such as :first-child and :last-child within $() to help you select the required DOM element.

See also

  • Selecting elements
You have been reading a chapter from
jQuery 2.0 Development Cookbook
Published in: Feb 2014
Publisher: Packt
ISBN-13: 9781783280896
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