Skip to content

Instantly share code, notes, and snippets.

@Xodarap
Created May 21, 2011 14:40
Show Gist options
  • Save Xodarap/984574 to your computer and use it in GitHub Desktop.
Save Xodarap/984574 to your computer and use it in GitHub Desktop.
jQuery toggle attribute
(function ($) {
$.fn.toggleAttr = function (attr, val1, val2) {
///<summary>Toggles an attribute between having one of two possible states</summary>
///<param name="attr">Attribute name</param>
///<param name="val1">First value</param>
///<param name="val2">Second value</param>
return this.each(function () {
var $this = $(this);
if ($this.attr(attr) === val1) {
$this.attr(attr, val2);
} else {
$this.attr(attr, val1);
}
});
};
}
)(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment