Document Object Model (DOM) Flashcards
Adding Javascript to…
Websites
Adding Inline Javascript (NOT GOOD PRACTICE. DON’T USE INLINE JS)
Inside the , add a onload=” ”
Example
Internal Javascript
Include a inside of the html
External Javascript
Include a with a src=” ”
The placement of the with the .js link matters!
Include CSS in the beginning, inside the
Include JavaScript at the bottom, just before the closing tag
Intro to the Document Object Model
Catalogs a webpage into individual models/objects where we can alter/manipulate its behaviour and style
Document is the root of the webpage
document.firstElementChild;
HTML Root of web page
document. firstElementChild.firstElementChild;
document. firstElementChild.lastElementChild;
Selecting HTML Elements with…
JavaScript
GetElementBy
(Broad selector)
Get elements by tag name (li, ul, h1, ….)
document.getElementsByTagName(< tag >);
Get elements by class name
document.getElementsByClassName(< className >);
Get element by id
document.getElementById(< id >);
Query Selector
(More complex and precise selector)
document.querySelector(< CSS Selector >);
Returns a singular html element
If the query selector obtains multiple elements, the querySelector only returns the first element
document.querySelectorAll( < CSS Selector > );
Returns a list of all the html elements that match the CSS Selector