Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle an element's attribute with jQuery?

Building a tree menu with jQuery, and I want it to be accessible, so I'm using aria attributes. What I want to do is toggle the "aria-expanded" attribute from true to false on click/enter. I've tried this, but it's obviously not correct:

$(this).closest('ul').find('> li.tree-parent').toggleAttr( 'aria-expanded', 'true false' );
like image 911
DeanH Avatar asked Dec 01 '22 15:12

DeanH


1 Answers

You can use .attr() to manually write the toggle logic

$(this).closest('ul').find('> li.tree-parent').attr('aria-expanded', function (i, attr) {
    return attr == 'true' ? 'false' : 'true'
});
like image 188
Arun P Johny Avatar answered Dec 09 '22 21:12

Arun P Johny