Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Alternative to chaining multiple .next() methods

I have a jQuery code like this:

$(this).next().next().next().next().html('<span>anything</span>');

Now I want to know is there any alternative for those .next() functions? (something like 4*next())

Note: .nextUntil() is not useful for me, Because I don't have any clue to use it in .nextUntil(). (My HTML structure is dynamic. In other word, it is not constant. Sometimes the final element is a span, and sometimes it is a div and so on ...)


Edit: Here is a few examples for my HTML structure:

example1:

<button>click it</button>
<div>div1</div>
<div>div2</div>
<span>span1</span>
<a>a1</a>              <!-- target !! and it should be <span>anything</span> -->
<div>div3</div>

example2:

<button>click it</button>
<span>span1</span>
<b>b1</b>
<span>span2</span>
<div>div1</div>       <!-- target !! and it should be <span>anything</span> -->
<div>div2</div>
<div>div3</div>
like image 690
Shafizadeh Avatar asked Dec 07 '15 02:12

Shafizadeh


People also ask

What is method chaining in jQuery?

The jQuery provides another robust feature called method chaining that allows us to perform multiple action on the same set of elements, all within a single line of code. This is possible because most of the jQuery methods return a jQuery object that can be further used to call another method. Here's an example.

How to run multiple jQuery methods on the same element?

Chaining allows us to run multiple jQuery methods (on the same element) within a single statement. Until now we have been writing jQuery statements one at a time (one after the other). However, there is a technique called chaining, that allows us to run multiple jQuery commands, one after the other, on the same element (s).

What are the different jQuery alternatives?

Below is the Different jQuery Alternatives which are as follows: 1. Javascript Nevertheless, Native javascript is one of the best jQuery alternatives, in fact, It is the framework of JS. Javascript is the best because any browsers ships with javascript and you do not need to install jQuery in your application.

What is the use of next method in HTML?

Definition and Usage. 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. Related methods:


1 Answers

You could combine the .nextAll()/.eq() methods:

$(this).nextAll().eq(3);

As a side note, the .eq() method accepts an index that is zero-based, which means that .eq(3) will select the fourth element.

Example:

$('div.nextAll span:first').nextAll().eq(3).addClass('selected');

$('div.multipleNext span:first').next().next().next().next().addClass('selected');
.selected {
  color: #f00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="nextAll">
  <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span>
</div>

<div class="multipleNext">
  <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span>
</div>

Alternatively, as pointed out in the comments, you could also use a combination of the general sibling combinator, ~ and the :eq() selector:

$('~:eq(3)', this);

or:

$(this).find('~:eq(3)');

Example:

$('~:eq(3)', 'div.nextAll span:first').addClass('selected');

$('div.multipleNext span:first').next().next().next().next().addClass('selected');
.selected {
  color: #f00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="nextAll">
  <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span>
</div>

<div class="multipleNext">
  <span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span>
</div>
like image 131
Josh Crozier Avatar answered Nov 15 '22 03:11

Josh Crozier