Skip to content

HTMLFormElement elements does not contain form-associated CustomElements when name attribute is shared #5891

@coemans

Description

@coemans

My first issue here, hopefully according to the guidelines...

I've noticed that the HTMLFormElement elements function does not contain my form associated CustomElements when the name attribute is shared over multiple form-associated (custom) elements.

<form>
    <custom-checkbox name="checkbox1"></custom-checkbox>
    <custom-checkbox name="checkbox2"></custom-checkbox>
    <input type="checkbox" name="checkbox3">
    <input type="checkbox" name="checkbox4">
</form>

When I request the form elements:

document.forms[0].elements['checkbox1']; // works like a charm
document.forms[0].elements['checkbox2']; // works like a charm
document.forms[0].elements['checkbox3']; // works like a charm
document.forms[0].elements['checkbox4']; // works like a charm

So far so good, but when I use the same name on all elements, only the native ones are returned in the HTMLFormControlsCollection.

<form>
    <custom-checkbox name="checkboxes"></custom-checkbox>
    <custom-checkbox name="checkboxes"></custom-checkbox>
    <input type="checkbox" name="checkboxes">
    <input type="checkbox" name="checkboxes">
</form>

When I request the form elements:

document.forms[0].elements['checkboxes'] // returns RadioNodeList(2) [input, input, value: ""]

As you can see the RadioNodeList only contains the 2 native input elements. Is there something missing in my CustomElement described below or did I run into a bug?

export class CustomCheckbox extends HTMLElement {
  static get formAssociated() {
    return true;
  }

  constructor() {
    super();
    this.attachShadow({mode: 'open'});
    this.shadowRoot.innerHTML = `<input type="checkbox"/>`;
    this._internals = this.attachInternals();
  }

  get form() {
    return this._internals.form;
  }

  get name() {
    return this.getAttribute('name');
  }

  get type() {
    return 'checkbox';
  }

  get validity() {
    return this._internals.validity;
  }

  get validationMessage() {
    return this._internals.validationMessage;
  }

  get willValidate() {
    return this._internals.willValidate;
  }

  get checked() {
    return this._inputElement.checked;
  }

  set checked(value) {
    this._inputElement.checked = value;
  }

  get _inputElement() {
    return this.shadowRoot.querySelector('input');
  }
}

customElements.define('custom-checkbox', CustomCheckbox);

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: custom elementsRelates to custom elements (as defined in DOM and HTML)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions