HTML inputs directly toggling CSS classes on elements?

# Alex Vincent (a day ago)

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] ajvincent/es7-membrane/blob/master/docs/distortions-gui/stylesheet.js#L13

Contact us to advertise here
# Roger Hågensen (11 hours ago)

On 2017-09-09 18:41, Alex Vincent wrote:

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.

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.

developer.mozilla.org/en-US/docs/Web/CSS/:checked

# Jonathan Zuckerman (9 hours ago)

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.

On Sun, Sep 10, 2017 at 7:25 AM Roger Hågensen <rh_whatwg at skuldwyrm.no>

wrote:

Want more features?

Request early access to our private beta of readable email premium.