How To Use elem.setAttribute

2020-02-02HTML1 min read

I was working on some ARIA stuff today and needed to toggle aria-expanded between true and false states.

I spent 3 hours banging my head because I was doing this

element.setAttribute("aria-expanded", !element.getAttribute("aria-expanded"))

I knew I was getting it right, but nothing worked. I later figured element.getAttribute() returns a string, while I was expecting a boolean. element.setAttribute() also requires a string as the second argument, and there I was passing a boolean.

Maybe it was worth it

I don't know about that, but next time element.setAttribute or element.getAttribute gives you a hard time, think in strings!.