0

Assume I have this html:

<main>
    <form>
        <label for="field">Label</label>
        <input id="field" type="number" min="0" required>
    </form>
</main>

In javascript if I type field and press enter, it'll give me the reference to the input element:

Example

Why this is happening? I can't find anything related

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51
  • 1
    I'm pretty sure that elements with an ID are added to the window object. I'm just trying to find that reference. https://2ality.com/2012/08/ids-are-global.html – evolutionxbox Feb 07 '22 at 12:11
  • 1
    See specs [Named access on the Window object](https://html.spec.whatwg.org/multipage/window-object.html#named-access-on-the-window-object)! – stacj Feb 07 '22 at 12:13
  • 1
    Browsers add global variables for elements with `id` values (and sometimes others), but **relying on them is poor practice**, for several reasons, one of which is that some names (like `id="name"` or `id="alert"`) *don't* get elements assigned to them because they have other meanings (conflicts). (Another is that a variable that seems to spring out of nowhere is difficult for people maintaining the code.) – T.J. Crowder Feb 07 '22 at 12:14
  • Thank you guys, if one answer I'll vote as the best. – João Paulo Marinho Izar Feb 07 '22 at 12:19

0 Answers0