Giter VIP home page Giter VIP logo

jquery-toggleattr's Introduction

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

jquery-toggleattr's People

Watchers

John Walker avatar Craig Roberts avatar James Cloos avatar Alan Burgoyne avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.