A simple jQuery plugin for toggling attributes, similar to how `toggleClass` works for CSS classes. Created so that `if-else` constructs like the following:
npm install @frogeducation/jquery-toggleattrA simple jQuery plugin for toggling attributes, similar to how toggleClass
works for CSS classes. Created so that if-else constructs like the following:
``javascript`
// 'unsavedChanges' is a boolean value
if (unsavedChanges) {
$('button#save').removeAttr('disabled');
} else {
$('button#save').attr('disabled', 'disabled');
}
can become:
`javascript
$('button#save').toggleAttr('disabled', 'disabled', !unsavedChanges);
`
Include the jQuery library first, and then the toggleAttr plugin:
Call toggleAttr on an element:
`javascript`
$('#someElement').toggleAttr(
The only required parameter is - and are
both optional. is used for the same purpose as the optional secondtoggleClass
parameter to jQuery core's - if true, it forces the attribute to
be added; if false, it forces the attribute to be removed.
HTML before:
``
JS:
`javascript`
$('input').toggleAttr('myattr');
HTML after:
``
JS:
`javascript`
$('input').toggleAttr('myattr');
HTML after:
``
HTML before:
``
JS:
`javascript`
$('div').toggleAttr('myattr', 'myValue');
HTML after:
``
HTML before:
``
JS:
`javascript`
$('div').toggleAttr('myattr', 'myValue', true);
HTML after:
``
If the parameter is true and the attribute already exists, the value
will be updated to :
JS:
`javascript`
$('div').toggleAttr('myattr', 'myNewValue', true);
HTML after:
```
The plugin is licensed under the MIT License.
- v1.0.0: First release