Socket
Socket
Sign inDemoInstall

@frogeducation/jquery-toggleattr

Package Overview
Dependencies
0
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @frogeducation/jquery-toggleattr

A simple jQuery plugin for toggling attributes, similar to how `toggleClass` works for CSS classes. Created so that `if-else` constructs like the following:


Version published
Maintainers
1
Install size
9.08 kB
Created

Readme

Source

jQuery toggleAttr

A simple jQuery plugin for toggling attributes, similar to how toggleClass works for CSS classes. Created so that if-else constructs like the following:

// 'unsavedChanges' is a boolean value
if (unsavedChanges) {
    $('button#save').removeAttr('disabled');
} else {
    $('button#save').attr('disabled', 'disabled');
}

can become:

$('button#save').toggleAttr('disabled', 'disabled', !unsavedChanges);

Installing

Include the jQuery library first, and then the toggleAttr plugin:

<script src="toggleattr.jquery.js" type="text/javascript></script>

Usage

Call toggleAttr on an element:

$('#someElement').toggleAttr(<attrName>, <attrValue>, <stateVal>)

The only required parameter is <attrName> - <attrValue> and <stateVal> are both optional. <stateVal> is used for the same purpose as the optional second parameter to jQuery core's toggleClass - if true, it forces the attribute to be added; if false, it forces the attribute to be removed.

Examples

Toggle an attribute with no value:

HTML before:

<input type="text" />

JS:

$('input').toggleAttr('myattr');

HTML after:

<input type="text" myattr />

JS:

$('input').toggleAttr('myattr');

HTML after:

<input type="text" />

Toggle an attribute with a value:

HTML before:

<div></div>

JS:

$('div').toggleAttr('myattr', 'myValue');

HTML after:

<div myattr="myValue"></div>

Toggle an attribute with the stateVal parameter

HTML before:

<div myattr="myValue"></div>

JS:

$('div').toggleAttr('myattr', 'myValue', true);

HTML after:

<div myattr="myValue"></div>

If the <stateVal> parameter is true and the attribute already exists, the value will be updated to <attrValue>:

JS:

$('div').toggleAttr('myattr', 'myNewValue', true);

HTML after:

<div myattr="myNewValue"></div>

License

The plugin is licensed under the MIT License.

Changelog

  • v1.0.0: First release

Keywords

FAQs

Last updated on 07 May 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc