Access the DOM in jQuery

jQuery is a very powerful tool. It provides various DOM access methods (DOM Traversal Method), which helps us to select elements in a document randomly or in a continuous manner.

Most DOM Traversal Method does not modify jQuery objects and they are used to filter their elements from a document based on the given conditions.

Search for elements by index in jQuery

Consider the following example of a simple document with the following HTML content:

 The JQuery Example 
 list item 1 list item 2 list item 3 list item 4 list item 5 list item 6 

It will produce the following result:

  1. list item 1
  2. list item 2
  3. list item 3
  4. list item 4
  5. list item 5
  6. list item 6

Above, each list has its own index, and can be located using the eq (index) method as in the example below.

Each child element starts its index from 0, so list item 2 will be accessed using $ ("li"). Eq (1) and so on.

For example

Simple example after adding color to the third item list .

 The JQuery Example  type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" >  type = "text/javascript" language = "javascript" > $ ( document ). ready ( function () { $ ( "li" ). eq ( 2 ). addClass ( "selected" ); }); 
 list item 1 list item 2 list item 3 list item 4 list item 5 list item 6 

It will produce the following result:

  1. list item 1
  2. list item 2
  3. list item 3
  4. list item 4
  5. list item 5
  6. list item 6

Filter elements in jQuery

The filter (selector) method can be used to filter out all elements from the matched set of elements that do not match the given Selector, and the selector can be written using any syntax. Which Selector.

For example

The following simple example applies colors to lists that are linked to the middle class:

 The JQuery Example  type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" >  type = "text/javascript" language = "javascript" > $ ( document ). ready ( function () { $ ( "li" ). filter ( ".middle" ). addClass ( "selected" ); }); 
  class = "top" > list item 1  class = "top" > list item 2  class = "middle" > list item 3  class = "middle" > list item 4  class = "bottom" > list item 5  class = "bottom" > list item 6 

It will produce the following result:

  1. list item 1
  2. list item 2
  3. list item 3
  4. list item 4
  5. list item 5
  6. list item 6

Locate child elements in jQuery

The find (selector) method can be used to locate all child elements of a particular element type. The selector can be written using any selector syntax.

For example

The following simple example selects all available elements inside the elements

different.

 The JQuery Example  type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" >  type = "text/javascript" language = "javascript" > $ ( document ). ready ( function () { $ ( "p" ). find ( "span" ). addClass ( "selected" ); }); 
 This is 1st paragraph and THIS IS RED 

This paragraph is 2nd andTHIS IS ALSO RED

It will produce the following result:

 This is 1st paragraph and THIS IS RED 
This paragraph is 2nd and THIS IS ALSO RED

The DOM Filter methods in jQuery

The table below lists useful methods that you can use to filter out various elements from a list of DOM elements:

Method Description eq (index)

Shorten the set of matching elements into a single element.

filter (selector)

Remove all elements from the matching set of elements that do not match the given selector.

filter (fn)

Remove all elements from the matched set of elements that do not match the given specific function.

is (selector)

Check the current selection with an Expression and return true, if at least one element of that selection matches the given selector.

map (callback)

Translates a set of jQuery object elements into another set of values ​​in a jQuery array (if possible or contains no elements).

not (selector)

Remove all elements that match the selector given from the set of matching elements.

slice (start, [end])

Select a subset of matching elements.

DOM methods Traversing in jQuery

The table below lists all the useful methods you can use to locate diverse elements in a DOM:

Format & Description1 add (selector)

Add multiple elements, matched by the given selector, to the set of matching elements.

2 andSelf ()

Add pre-selection to current selection

3 children ([selector])

Get a set of elements that contain all of the only direct child elements of each of the matched elements.

4 closest (selector)

Get a set of elements that contain the closest parent element that matches the given selector, including the starting element

5 contents ()

Find all child nodes within matching elements (including text nodes), or document content, if the element is an Iframe.

6 end ()

Transform the most recent destructive operation, changing the set of elements to the previous state.

7 find (selector)

Search for child elements that match the given selector.

8 next ([selector])

Get a unique set of next brother (em) elements of each element in the given element set.

9 nextAll ([selector])

Find all siblings after the current element

10 offsetParent ()

Returns a jQuery set with the specified parent element of the first matched element

11 paren ([selector])

Get direct father of an element. If called on a set of elements, the parent method returns a unique set of direct parent elements

12 parents ([selector])

Get a set of ancestor elements of the set of matching elements (except for root elements)

13 prev ([selector])

Get a set of elements containing the only preceding element brother of each element in the set of matching elements.

14 prevAll ([selector])

Find all brother elements before the current element

15 siblings ([selector])

Get a set of elements that contain all of the only brother elements of each element in the given set of elements

Follow tutorialspoint

Previous article: Attributes in jQuery

Next article: CSS Selector in jQuery

4 ★ | 2 Vote

May be interested

  • How to Use jQuery on Your WebsiteHow to Use jQuery on Your Website
    jquery is the most popular javascript language by far, used by many popular websites across the internet. however, since it is a library, it is not a part of unaltered javascript. how, then, can you use jquery on your website? the process...
  • jQuery AjaxjQuery Ajax
    ajax stands for asynchronous javascript and xml and this technology helps us to download data from the server without refreshing the browser.
  • Tooltip creation tools are useful for jQueryTooltip creation tools are useful for jQuery
    tooltip is a tool that displays a caption for an image or a piece of text when moving the cursor to that text or image. the following article will list useful tooltip creation tools with jquery!
  • MS Access 2003 - Lesson 1: Chapter 1: Terminology AccessMS Access 2003 - Lesson 1: Chapter 1: Terminology Access
    this chapter is the first step in learning and familiarizing with basic terms, as well as how to start and exit access. you will get help and master the outline of what you can do with access.
  • Unable to access 192.168.1.1: Cause and remedyUnable to access 192.168.1.1: Cause and remedy
    why can't i go to 192.168.1.1? it is a question that many people are interested in. to fix bugs that cannot be accessed 192.168.1.1, follow the instructions below.
  • Calculation expressions in Access 2016Calculation expressions in Access 2016
    expressions in ms access can be understood similarly to formulas in excel. expressions can include operators, constants, functions, and identifiers.
  • Get familiar with Acccess 2016 interface and basic operationsGet familiar with Acccess 2016 interface and basic operations
    if you have used access 2013 or 2010 you will be familiar with the access 2016 interface: ribbon ribbon and quick access toolbar (providing commands to perform common tasks in access) as well as file menu.
  • What is an Access Point? Should I use an Access Point, Router or Modem?What is an Access Point? Should I use an Access Point, Router or Modem?
    provides necessary information about the structure, operation and function of the access point, and compares it with devices such as routers and modems. click to see!
  • MS Access 2003 - Lesson 5: Chapter 2: Access Learning in 1 hourMS Access 2003 - Lesson 5: Chapter 2: Access Learning in 1 hour
    access is a powerful, flexible and easy-to-use database management program. in this chapter, you will start to mention access's capabilities.
  • Work with data in Access 2016Work with data in Access 2016
    in the previous lesson, you became familiar with the access 2016 interface as well as the opening and closing of the basic database. this article will continue to go deeper into access 2016 around the content of working with data in access 2016.