Discussion:
[whatwg] HTML inputs directly toggling CSS classes on elements?
Alex Vincent
2017-09-09 16:41:34 UTC
Permalink
A few days ago, I dipped my toes into web design again for the first time
in a while. One of the results is the CSSClassToggleHandler constructor
from [1]. Basically, it takes an radio button or checkbox, and turns that
input into a toggle for a CSS class on another element.

This is relatively easy to do in JavaScript, as the very short function
illustrates. I wonder out of simple curiousity if anyone's considered
defining a short set of attributes to do this in HTML itself, without
requiring JavaScript.

Three attributes on the input would be all that's necessary:

- cssClassName="(word)" would be the class name to apply
- cssClassFor="(id)" would be an IDREF to the element which would apply
the class
- cssClassNot="true" would invert the class enable/disable (so that if
the input is checked, the class would be removed instead of applied)

I'm of two minds about this.

On the one hand, if HTML can provide a reference from an input to an
element the input is intended to influence, that has to be useful for
reasons similar to the HTML label element's for attribute, and it makes for
one less dependency on JavaScript. (Accessibility?)

On the other hand, the modern Web has so much dependency on JavaScript.
Also, the HTML input element has a horrendously long list of attributes on
it already.

So, I thought I'd throw the idea out there and see if anyone likes it.

Alex

[1]
https://github.com/ajvincent/es7-membrane/blob/master/docs/distortions-gui/stylesheet.js#L13-L26
--
"The first step in confirming there is a bug in someone else's work is
confirming there are no bugs in your own."
-- Alexander J. Vincent, June 30, 2001
Jonathan Zuckerman
2017-09-10 13:31:19 UTC
Permalink
class names are meant to be a tiny wormhole which connects the worlds of
content (HTML), presentation (CSS), and behavior (JS) - I think this
suggestion begins to widen that rip, and it's inadvisable. It's a question
of taste I guess, just which behaviors are primitive enough to not require
javascript.

As you've proven, this idea is easily implemented in Javascript. If you
were to get an incredible rate of adoption for that script, it might
indicate that there is widespread demand for this feature, and you'd be
able to make a case that it's worth implementing in the browser.
Post by Alex Vincent
A few days ago, I dipped my toes into web design again for the first time
in a while. One of the results is the CSSClassToggleHandler constructor
from [1]. Basically, it takes an radio button or checkbox, and turns
that
Post by Alex Vincent
input into a toggle for a CSS class on another element.
You do have :checked
While I haven't used that with radio or checkboxes much myself it seems
to at least partially do what you are describing.
https://developer.mozilla.org/en-US/docs/Web/CSS/:checked
--
Unless specified otherwise, anything I write publicly is considered
Public Domain (CC0). My opinions are my own unless specified otherwise.
Roger Hågensen,
Freelancer, Norway.
Loading...