Find the next element that comes later in DOM

cregox

Probably better in javascript, but this can sure include jQuery, or any such library.

I want to find the first .next in the example below.

There are a lot of answers to similar questions that suggest nextAll or siblings... Both are useless here:

$(function(){
  $('.result').text(
    $('.origin').nextAll('.next').text()
      || $('.origin').siblings('.next').text()
      || 'both failed'
  )
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="whatever">
  <p class="result"></p>
  <p class="origin">1</p>
</div>
<p class="next">2</p>
<p class="next">3</p>

Also, what'd be the most compatible (browser and library wise) and most performatic (speed and less lines of code) way to do it so?

cregox

Based on the awesome answer by @mrtsherman, I wrote this more complete solution and tested it to work on Chrome and Safari:

$(function() {
  $('.result').text(
    $('.origin').below('.next').text()
  );
});

(function($) {
  $.fn.below = function(sel) {
    var $result = $(sel).first();
    if ($result.length <= 0) {
      return $result;
    }
    $result = [];
    var thisIndex = $('*').index($(this));
    var selIndex = Number.MAX_VALUE; // Number.MAX_SAFE_INTEGER is not yet fully supported
    $(sel).each(function(i, val) {
      var valIndex = $('*').index($(val));
      if (thisIndex < valIndex && valIndex < selIndex) {
        selIndex = valIndex;
        $result = $(val);
      }
    });
    return $result;
  };
})(jQuery);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="whatever">
  <p class="result"></p>
  <p class="origin">1</p>
</div>
<p class="next">2</p>
<p class="next">3</p>

I will probably try to submit a PR for this on jQuery lib, because I believe...

This should be a core method on jQuery! :P


In fact, I just ran a very simplistic test on this, and it performed even better than nextAll for this simple example. Almost twice as fast! :o

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find Next Sibling Element In DOM With JavaScript

From Dev

Find next element in jquery

From Dev

Unable to find next element

From Dev

Javascript dom selection of next element

From Dev

Get ID of next DOM element

From Dev

Select the next DOM element after the current element

From Dev

Change Div which comes in DOM before UL-element

From Dev

Change Div which comes in DOM before UL-element

From Dev

Find the next greater element in an array

From Dev

Find the next greater element in an array

From Dev

jQuery, find next element with id

From Java

Find javascript that is changing DOM element

From Dev

How to traverse to next dom element with specific class?

From Dev

Find Element Next to Element With Specified Text

From Dev

How to find element next to another element?

From Dev

Find the pair that gives the biggest sum such that second element comes after first

From Dev

angular.element() cannot find DOM element

From Dev

Find element and re-use it later capybara cucumber

From Dev

Find next greater element in AVL tree

From Dev

Find next select element inside class

From Dev

Find first next element and then stop the search

From Dev

jQuery: Find next Element with same class

From Dev

Find next element out of parent using jQuery

From Dev

jquery find next element without attribute

From Dev

Jquery - find next element in each loop

From Dev

jquery find next element with class name

From Dev

How to find the next element in a generic List?

From Dev

Find an element in DOM at given position by class

From Dev

Find closest element in the DOM with multiple selector