DOM - Document Object Model
Goal
- âš¡ Understand (1) of the below code
let
?
What is let
is used to declare variable.
Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
caution
There is also var
to declare variable. But currently it is enough to use let
.
document
?
What is To understand document
, we need to understand DOM - Document Object Model
What is DOM ?
When browser receives HTML file, the browser turns HTML file into a Document Object Model (DOM) Tree under browser's window object.
ref: https://developers.google.com/web/updates/2018/09/inside-browser-part3
Why making DOM ?
By making DOM tree, it becomes easier to handle by javascript.
document
Check Let's check document
in chrome console.
In console
tab, you can write javascript
code.
And you can easily access document
thanks to DOM tree.
By typing document
in console, you can obtain tree under document
.
getElementById()
method
As described in the below link document
has various "methods".
https://developer.mozilla.org/en-US/docs/Web/API/Document
getElementById()
is one of these document
's method.
The Document method
getElementById()
returns anElement
object representing the element whose id property matches the specified string.
https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementById
getElementById()
Image of This is the image of getting element by using getElementById()
.
Check returned Element
returns the below part of Element
object.
Summery
I hope you understand (1)
Recap the meaning of this code
- Declare
demoElement
variable usinglet
- Assign
Element
object which is returned bydocument.getElementById("demo")
to it.