2

I accidentally discovered that you can access element just typing it's id e.g:

<input id="test" value="testValue"/>
<script>
   console.log(test.value); //testValue
</script>

Can anyone point me please where that feature coming from? (it is really hard to google things like that)

Zulu
  • 8,765
  • 9
  • 49
  • 56
Vladimirs
  • 8,232
  • 4
  • 43
  • 79
  • 2
    It was something that Internet Explorer started doing way, way back, and a huge amount of code was written to expect that behavior so that's the way other browsers work now. It's a shame because it's an absolutely terrible idea. – Pointy Feb 29 '16 at 20:00
  • 1
    wow that's amazing! – user2950593 Feb 29 '16 at 20:01
  • 1
    @vladimirs in addition, since you said it would be really hard to search this in Google, I give you a hint to do it yourself: http://goo.gl/6B0gS3 – Matías Fidemraizer Feb 29 '16 at 20:03

1 Answers1

3

From the spec:

window[name]

Returns the indicated element or collection of elements.

As a general rule, relying on this will lead to brittle code. Which IDs end up mapping to this API can vary over time, as new features are added to the Web platform, for example. Instead of this, use document.getElementById() or document.querySelector().

Community
  • 1
  • 1
Mike Cluck
  • 31,869
  • 13
  • 80
  • 91