Determine If Element Is After Another Element - isAfter

File Size: 5.57 KB
Views Total: 1529
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
Determine If Element Is After Another Element - isAfter

isAfter is an easy yet useful jQuery plugin that detects if an element is after another element rendered in the DOM tree.

Unlike the jQuery prevAll() and nextAll() methods, the plugin enables you to compare the rendering order of two DOM elements that belong to different parent containers.

How to use it:

1. Download and insert the JavaScript file isafter.jquery.js after jQuery library.

<script src="" 
<script src="isafter.jquery.js"></script>

2. Check if an element (element1) is after another (element2).


3. The full example.

<div class="example" id="Example">
  <div class="test-html">
      <p class="first">
          [<i> div ></i> <b>div > p.first</b>]
        I'm first
    <p class="second clearfix">
        [<i> div ></i> <b>p.second</b>]
      I'm second
  <div class="message"></div>
function prepareMessage(parentSelector)
  var $parent = $(parentSelector);
  var $first = $parent.find("p.first");
  var $second = $parent.find("p.second");

  var test1 = $second.isAfter($first);
  var test2 = ($second.prevAll($first).length !== 0);

  if(test1 !== test2)
    .append("<i>p.first</i> after <i>p.second</i>? ")
    .append("using <i>isAfter</i>: ")
    .append("<b>" + (test1? "yes":"no") + "</b>")
    .append(", using <i>prevAll().length</i>: ")
    .append("<b>" + (test2? "yes":"no") + "</b>");

  function (){

This awesome jQuery plugin is developed by arashdalir. For more Advanced Usages, please check the demo page or visit the official website.