Skip to content

Instantly share code, notes, and snippets.

@joe-watkins
Last active April 22, 2016 06:28
Show Gist options
  • Save joe-watkins/00a1bd83fec29048f4ed to your computer and use it in GitHub Desktop.
Save joe-watkins/00a1bd83fec29048f4ed to your computer and use it in GitHub Desktop.
toggleAria.js - a small plugin for swapping boolean aria attributes easier
/*!
* toggleAria.js
* Author: Joe Watkins
* usage:
HTML
<button aria-pressed="false">Show Content</button>
$("button").on("click", function(){
$(this).toggleAria({aria: "pressed"});
});
*
*/
(function($){
$.fn.toggleAria = function(options) {
var defaults = {
wrapper: this,
aria: ""
},
options = $.extend(defaults, options),
o = options,
currentState = $(o.wrapper).attr("aria-"+o.aria),
newState;
if(currentState == "false"){
newState = "true";
}else{
newState = "false";
}
$(o.wrapper).attr("aria-"+o.aria,newState);
}; // $.fn
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment