Last updated 2 years ago
Was this helpful?
JS โฉ scope โฉ global โฉ DOM element with "id"
DOM element with the "id" attribute.
DOM element with "id" is registerd as global variable/global object property automatically.
Element โฉ attribute โฉ id
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">
HTML โฉ Global attributes โฉ id
Web APIs โฉ Element โฉ .id
CSS โฉ ID selectors
YDKJS: Scope & Closures (v.2) โฉ Chapter 4: Around the Global Scope