[whatwg] HTML inputs directly toggling CSS classes on elements?

Alex Vincent ajvincent at gmail.com
Sat Sep 9 09:41:34 PDT 2017

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.



"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

More information about the whatwg mailing list