๐ธDOM element with "id"
JS โฉ scope โฉ global โฉ DOM element with "id"
DOM element with "id" is registerd as global variable/global object property automatically.
replit๏ผDOM globals
๐ index.html
<!--โญโโโโโ id โโโโโโโฎ -->
<ul id="my-todo-list">
<!--โญโโ id โโโฎ -->
<li id="first">Write a book</li>
</ul>๐ script.js
// โญ๏ธ case 1: "id" is valid "identifier"
first, // <li id="first"> // global variable โญ๏ธ
window.first, // <li id="first"> // dot notation โญ๏ธ
window["first"], // <li id="first"> // bracket notation
// โญ๏ธ case 2: "id" is invalid as "identifier"
window["my-todo-list"], // <ul id="my-todo-list">Last updated
Was this helpful?