Skip to content

Instantly share code, notes, and snippets.

@mathiasbynens
Created February 8, 2010 21:20
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mathiasbynens/298591 to your computer and use it in GitHub Desktop.
Save mathiasbynens/298591 to your computer and use it in GitHub Desktop.
toggleAttr() jQuery plugin
/*!
* toggleAttr() jQuery plugin
* @link http://github.com/mathiasbynens/toggleAttr-jQuery-Plugin
* @description Used to toggle selected="selected", disabled="disabled", checked="checked" etc…
* @author Mathias Bynens <http://mathiasbynens.be/>
*/
jQuery.fn.toggleAttr = function(attr) {
return this.each(function() {
var $this = $(this);
$this.attr(attr) ? $this.removeAttr(attr) : $this.attr(attr, attr);
});
};
@victor-rocha
Copy link

victor-rocha commented Jul 13, 2016

$(selector).attr('disabled', function (_, attr) { return !attr });

This works (at least to enable or disable fields) as well!

@ghatolamol
Copy link

@viicviicviic

jQuery(selector).attr('required', function (_, attr) { return !attr });

This working for required too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment