Skip to content

Add class & for aliases for className and htmlFor #9379

Description

@jakearchibald

https://dom.spec.whatwg.org/#dom-element-classname
https://html.spec.whatwg.org/multipage/forms.html#dom-label-htmlfor

I assume these were originally given unusual and inconsistent names to avoid reserved words, but it isn't an issue with modern JavaScript.

Could class be added to alias className, and for to alias htmlFor? The old names would be considered legacy.

It's a minor thing, but it's polyfillable, and removes a weird inconsistency on the platform.

Activity

  1. tabatkins commented on Jun 4, 2023

    @tabatkins
    Contributor

    Yeah, pre-ES5 those keywords were reserved unconditionally. But it's been a decade since they were usable for property access.

  2. aminya commented on Jun 4, 2023

    @aminya

    Well, Solidjs, as an example, doesn't expose these names in JSX. It simply uses class. The fact that React is doing this seems like an issue that can be fixed on their part. So I do not think the HTML/DOM specification needs to change because of that.

  3. annevk commented on Jun 5, 2023

    @annevk
    Member

    What's a good use case for className that is not better tackled with classList? (Also, this part of the request impacts whatwg/dom, not whatwg/html.)

  4. gioboa commented on Jun 5, 2023

    @gioboa

    Well, Solidjs, as an example, doesn't expose these names in JSX. It simply uses class. The fact that React is doing this seems like an issue that can be fixed on their part. So I do not think the HTML/DOM specification needs to change because of that.

    Same for Qwik we can use class and for in JSX templete and it's so cosy

  5. jakearchibald commented on Jun 5, 2023

    @jakearchibald
    ContributorAuthor

    @annevk cases where you want to override previous changes, or setting an initial set of class names.

  6. Gpx commented on Jun 5, 2023

    @Gpx

    Well, Solidjs, as an example, doesn't expose these names in JSX. It simply uses class. The fact that React is doing this seems like an issue that can be fixed on their part. So I do not think the HTML/DOM specification needs to change because of that.

    I think this proposal should be considered on its own ignoring what JSX or other libraries are doing. When teaching how to manipulate the DOM this is often something that catches students off guard. IMO it makes sense to add an alias for these properties. It feels more natural to interact with for rather than htmlFor and the same goes for class.

  7. jakearchibald commented on Jun 5, 2023

    @jakearchibald
    ContributorAuthor

    I think this proposal should be considered on its own ignoring what JSX or other libraries are doing.

    Agreed. I didn't mention frameworks or JSX in the proposal for this reason.

  8. patrickhlauke commented on Jun 5, 2023

    @patrickhlauke
    Member

    I'd be in favour of the new shorter consistent properties, but for backwards compat likely need to keep the old ones as an alias (assuming that was the implicit intention with making them legacy)

  9. changed the title [-]Add class & for aliases for className and htmlFor[/-] [+]Add `class` & `for` aliases for `className` and `htmlFor`[/+] on Jun 5, 2023
  10. jakearchibald commented on Jun 5, 2023

    @jakearchibald
    ContributorAuthor

    The proposal is to add aliases, not rename the properties.

  11. patrickhlauke commented on Jun 5, 2023

    @patrickhlauke
    Member

    Doh, sorry, it's right there in the OP and I managed to miss it.

  12. hinell commented on Jun 5, 2023

    @hinell

    You should clarify your post on whether you are talking about HTML Attributes or Element properties.

  13. jakearchibald commented on Jun 5, 2023

    @jakearchibald
    ContributorAuthor

    @hinell added spec links. Although, I thought it would be obvious since className and htmlFor are properties, not attributes.

  14. 20 remaining items

  15. titoBouzout commented on May 4, 2025

    @titoBouzout

    This will break code that assumed class doesnt exists. Such

    const prop = 'class'
    const value = { red:true }
    
    if( prop in node ) {
      node[prop] = value
    } else if ( prop === 'class' ) {
     if( value.red ) {
      node.className = 'red'
     }
     //.... 
    }
  16. zcorpan commented on May 5, 2025

    @zcorpan
    Member

    @titoBouzout do you know of a JS library or so that does that?

  17. titoBouzout commented on May 5, 2025

    @titoBouzout

    No, I am brainstorming how this could break something. However, this svelte code comes very close:

    https://github.com/sveltejs/svelte/blob/0ace76d8f1ea5402087e202152630cd50a5d757a/packages/svelte/src/internal/client/dom/elements/attributes.js#L235-L248

    Besides whatever they are doing, I see.

    1. if(prop in setters) { node[prop] = value }
    2. else { if (attribute === 'loading') { ... } } (the set_attribute call)

    Similar example to what I gave before, they check for prop in before special casing some props, up to an extent tho.


    Looking around I am figuring that even if class is added, It would probably be a good idea to avoid using it because of the likelihood it has to be broken by some random stuff, like prototype.class = function.. somewhere.

    It becomes a thing of "if this project is new then use class, if its jQuery/htmx or whatever, then use className.".

    It also has the problem that however was supporting className, now has to also support class. As in, hooking className to do whatever magic they were doing, now they have to do the same magic for class.

    I honestly do not see the benefice of adding it, it will just cause random difficulties for little gain.

    Also, class is supported in most modern frameworks, they just map it once, is not that you see className everywhere.

  18. trusktr commented on May 6, 2025

    @trusktr

    className is currently defined on Element, but SVGElement overrides it.

    Nice, this means .class can on on Element and simply work for all elements. Let's keep this philosphy of consistency across all types of elements.

  19. trusktr commented on May 6, 2025

    @trusktr
    const prop = 'class'
    const value = { red:true }
    
    if( prop in node ) {
      node[prop] = value
    } else if ( prop === 'class' ) {
     if( value.red ) {
      node.className = 'red'
     }
     //.... 
    }

    This code will still work because .className will remain the same, and prop === 'class' (I'm assuming) is checking a value from templating (f.e. JSX) and not from the DOM.

    the likelihood it has to be broken by some random stuff, like prototype.class = function.. somewhere.

    I understand the concern, and I trust that from the comments above the implementers will follow through with the testing for how many apps break, to determine how doable this is. A handful of breaks won't be meaningful, but if for example some library would break and many dependent sites would go down, it could be significant enough to avoid.

  20. zcorpan commented on May 7, 2025

    @zcorpan
    Member

    @trusktr the if( prop in node ) branch would be taken if node now has a class property.

  21. noamr commented on May 17, 2026

    @noamr
    Contributor

    I wonder if folks on this thread have an opinion about what we should do with new occurrences of the for attribute and how they should be reflected, e.g. <template for> (#11818).

    My current thinking is that having element.for in some cases and element.htmlFor in other cases would do more harm than good, and we should use htmlFor in new cases and alias all of them once we resolve on this.

    But happy to hear other opinions.

  22. Psychpsyo commented on May 17, 2026

    @Psychpsyo
    Contributor

    My current thinking is that having element.for in some cases and element.htmlFor in other cases would do more harm than good, and we should use htmlFor in new cases and alias all of them once we resolve on this.

    But happy to hear other opinions.

    I remember there being a point made that legacy attributes that are only kept for compatibility shouldn't get this treatment at all. (like the for on script elements)

    I personally somewhat disagree because it sounds like an unfortunate reason to introduce random inconsistencies, but I thought I'd bring it up as an edge-case to be aware of.

    Otherwise this plan sounds sensible.

  23. jakearchibald commented on May 18, 2026

    @jakearchibald
    ContributorAuthor

    @noamr

    My current thinking is that having element.for in some cases and element.htmlFor in other cases would do more harm than good, and we should use htmlFor in new cases and alias all of them once we resolve on this.

    Agreed.

  24. zcorpan commented on May 18, 2026

    @zcorpan
    Member

    If it's compatible enough I think it would be nicer with for and class. Fixing https://issues.chromium.org/u/1/issues/367992694 would help, it has a CL but no activity since August 2025.

  25. keithamus commented on Jul 9, 2026

    @keithamus
    Member

    I had a little search on GitHub and found a few codebases that create label elements and set .for on them:

    https://github.com/6859-sp21/final-project-musical-ties/blob/main/app.js#L227-L230

    var label = document.createElement('label');  
    label.classList.add("form-check-label"); 
    label.className = "form-check-label"; 
    label.for = 'sortSelection-default';

    https://github.com/cwmaguire/js_doodle/blob/08a926a7051264d0640acc6f6f9be11fd5df5501/r.js#L241-L244

    let label = document.createElement('LABEL');
    label.id = name + '_label';
    label.innerText = name;
    label.for = name;

    https://github.com/hackspace-marburg/gluon-firmware-wizard/blob/61b7fdfa0cdead72cd757db4a5a9006a560a293e/app.js#L923-L924

    var label = document.createElement('label');
    label.for = 'radiogroup-typeselect-' + type;

    I don't think these are webcompat issues - actually quite the opposite, I think we'll be fixing these websites - however they will change in behaviour.

  26. noamr commented on Jul 9, 2026

    @noamr
    Contributor

    Note that in some cases like destructuring htmlFor is still useful, e.g. this is a syntax error:

    const {for, class, id} = my_label;
    console.log(for);

    So I think that even if we add for as an alias we'd want to keep htmlFor also for future occorunces of the for attribute.

  27. Psychpsyo commented on Jul 9, 2026

    @Psychpsyo
    Contributor

    I had a little search on GitHub and found a few codebases that create label elements and set .for on them:

    I don't think these are webcompat issues - actually quite the opposite, I think we'll be fixing these websites - however they will change in behaviour.

    I did this same search back in 2024 and came to a similar conclusion after looking through a few pages of results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions