Skip to content

Instantly share code, notes, and snippets.

@nocodesupplyco
Created December 19, 2022 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nocodesupplyco/3bb881a611ba0094d8f53fef2856ce90 to your computer and use it in GitHub Desktop.
Save nocodesupplyco/3bb881a611ba0094d8f53fef2856ce90 to your computer and use it in GitHub Desktop.
Toggle Accordion Attributes for Accessibility
$(".accordion-trigger").click(function () {
$(this).toggleAttrVal("aria-expanded", "false", "true"); //toggle trigger attribute
$(this)
.siblings(".accordion-content")
.toggleAttrVal("aria-hidden", "true", "false"); //toggle content attribute
});
// jquery toggle just the attribute value
$.fn.toggleAttrVal = function (attr, val1, val2) {
var test = $(this).attr(attr);
if (test === val1) {
$(this).attr(attr, val2);
return this;
}
if (test === val2) {
$(this).attr(attr, val1);
return this;
}
// default to val1 if neither
$(this).attr(attr, val1);
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment