Toggle Attribute From Element Using jQuery - toggleAttr.js

File Size: 3.26 KB
Views Total: 872
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Toggle Attribute From Element Using jQuery - toggleAttr.js

toggleAttr.js is a super tiny jQuery plugin that makes it possible to toggle a single or multiple attributes from a specific DOM element with a single JS call.

How to use it:

1. Load the jQuery-toggleAttr.js JavaScript library after jQuery.

<script src="/path/to/cdn/jquery.slim.min.js"></script>
<script src="/path/to/jQuery-toggleAttr.js"></script>

2. Remove the href attribute from a link.

<a href="https://www.google.com" id="linkExample">
  Example Link
</a>
$("#linkExample").toggleAttr("href");
<!-- Result: -->
<a id="linkExample">
  Example Link
</a>

3. Remove multiple attributes from that link.

<a class="btn" href="https://www.google.com" id="linkExample">
  Example Link
</a>
$("#linkExample").toggleAttr("href class attr3="value'");
<!-- Result: -->
<a id="linkExample">
  Example Link
</a>

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