본문 바로가기
✘✘✘ Web

Dom id => window[id] or document.querySelector('#id')

by PrettyLog 2022. 7. 22.

Why don't we just use element IDs as identifiers in JavaScript?

HTML 5 spec, 'Named access on window object"

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().

HTML

댓글