Javascript find next sibling with class

Get the immediately following sibling of each DOM element within a set of DOM elements.

Syntax

.next()
.next(selector)
.next(options)
.next(selector, options)

Usage

Correct Usage

cy.get('nav a:first').next() // Yield next link in nav

Incorrect Usage

cy.next() // Errors, cannot be chained off 'cy'
cy.getCookies().next() // Errors, 'getCookies' does not yield DOM element

Arguments

selector (String selector)

A selector used to filter matching DOM elements.

options (Object)

Pass in an options object to change the default behavior of .next().

OptionDefaultDescription
log true Displays the command in the Command log
timeout defaultCommandTimeout Time to wait for .next() to resolve before timing out

Yields

  • .next() yields the new DOM element(s) it found.

Examples

No Args

Find the element next to .second

<ul>
  <li>apples</li>
  <li class="second">oranges</li>
  <li>bananas</li>
</ul>

// yields <li>bananas</li>
cy.get('.second').next()

Testing a datalist

<input list="fruit" />
<datalist id="fruit">
  <option>Apple</option>
  <option>Banana</option>
  <option>Cantaloupe</option>
</datalist>

cy.get('#fruit option')
  .first()
  .should('have.text', 'Apple')
  .next()
  .should('have.text', 'Banana')
  .next()
  .should('have.text', 'Cantaloupe')

Selector

Find the very next sibling of each li. Keep only the ones with a class selected

<ul>
  <li>apples</li>
  <li>oranges</li>
  <li>bananas</li>
  <li class="selected">pineapples</li>
</ul>

// yields <li>pineapples</li>
cy.get('li').next('.selected')

Rules

Requirements

  • .next() requires being chained off a command that yields DOM element(s).

Assertions

  • .next() will automatically retry until the element(s) exist in the DOM
  • .next() will automatically retry until all chained assertions have passed

Timeouts

  • .next() can time out waiting for the element(s) to exist in the DOM .
  • .next() can time out waiting for assertions you've added to pass.

Command Log

Find the element next to the .active li

cy.get('.left-nav').find('li.active').next()

The commands above will display in the Command Log as:

Javascript find next sibling with class

When clicking on next within the command log, the console outputs the following:

Javascript find next sibling with class

See also

  • .nextAll()
  • .nextUntil()
  • .prev()

What is next sibling in Javascript?

nextSibling returns the next node (an element node, a text node or a comment node). Whitespace between elements are also text nodes. nextElementSibling returns the next element (not text and comment nodes).

How do you target siblings in Javascript?

First, select the parent of the element whose siblings that you want to find. Second, select the first child element of that parent element. Third, add the first element to an array of siblings. Fourth, select the next sibling of the first element.

How can I get next sibling in jQuery?

jQuery next() Method The next() method returns the next sibling element of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse forward along the next sibling of DOM elements.

Is used to select all the siblings of an element?

To select all sibling elements of an element, we will use siblings() method. The siblings() method is used to find all sibling elements of the selected element. The siblings are those having the same parent element in DOM Tree.