Re: [whatwg/dom] [idea] toggleAttribute with detault value when attribute is added. (#939)

I’d just like to add  a quick use-case. I have the following code in my project:

```js
// sets aria-current="page" on the current link in <nav>
updateNav(e) {
  for (let a of document.querySelectorAll('.Main-nav a')) {
    if (a.getAttribute('href') === e.url) {
      a.setAttribute('aria-current', 'page');
    } else {
      a.removeAttribute('aria-current');
    }
  }
}
```

As  you can see, based on the condition `a.getAttribute('href') === e.url`, I would like to set/remove the  `aria-current="page"` attribute on each link in my navigation element.

Instead of the above multi-line `if` statement, I would prefer a single function call,  something like

```js
a.toggleAttribute(['aria-current', 'page'], a.getAttribute('href') === e.url);
```

Would it be possible to extend the first parameter to accept an array or object for this purpose?

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/939#issuecomment-753426925

Received on Saturday, 2 January 2021 03:55:34 UTC