Get The Parent Of An Element - jQuery commonParent.js

Get The Parent Of An Element - jQuery commonParent.js
File Size: 3.36 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

commonParent.js is a small yet useful jQuery plugin for developers that have the ability to get the closest parent (or all parent elements) of a given element for testing/debugging use.

How to use it:

1. Insert the jQuery JavaScript library and the commonParent.js script into the document.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" 
        integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" 
        crossorigin="anonymous">
</script>
<script src="jquery.commonparent.js"></script>

2. Get the closest parent of a given element and output the result in the console.

<div class="container">
  <div class="row">
    <div class="col-md-4 grid">
      ... content here ...
    </div>
  </div>
</div>
var instance = $('.grid').commonParent();
console.log(instance);

3. Get all the parent of a given element and output the result in the console.

var instance = $('.grid').commonParents();
console.log(instance);

4. Determine whether or not to exclude the current element in the result.

$('.grid').commonParent(true);
$('.grid').commonParents(true);

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