At the end of this class, you will be able to:
wilson-espina/jsd-9-resources
repo to your computer:cd
to the Documents/JSD/jsd-9-resources
directorygit pull origin master
and press return
Documents/JSD/jsd-9-resources/08-intro-dom
View > Developer > Developer Tools
and click on the Elements panel to take a look at the DOM tree.Web Page Elements
DOM Tree
Web Page Elements
DOM Tree
Web Page Elements
DOM Tree
Key Objective
Type of Exercise
Timing
3 mins |
|
getElementById()
getElementsByClassName()
getElementsByTagName()
querySelector()
querySelectorAll()
Lets us select DOM elements using CSS selector syntax
'div'
div {
color: red;
}
'#main'
#main {
color: red;
}
'.button'
.button {
color: red;
}
'p, li'
p, li {
color: red;
}
'div a'
div a {
color: red;
}
Key Objective
Type of Exercise
Location
Timing
10 mins |
|
querySelector()
<body id="main">
...
</body>
document.querySelector('#main')
querySelector()
document.querySelector('li')
querySelectorAll()
document.querySelectorAll('li')
innerHTML
propertyid
, src
etc.)innerHTML
const item = document.querySelector('li')
console.log(item.innerHTML)
// Gets value: 'Lorem ipsum'
innerHTML
const item = document.querySelector('li')
item.innerHTML = 'Toast';
// Sets value: 'Toast'
className
propertyclass
attribute value.
const item = document.querySelector('li')
console.log(item.className);
// Gets value: ''
className
propertyclass
attribute value.
const item = document.querySelector('li')
item.className = 'selected';
// Sets value: 'selected'
classList.add
method
const item = document.querySelector('li')
item.classList.add('selected');
// Adds value: 'selected'
classList.remove
method
const item = document.querySelector('li')
item.classList.remove('selected');
// Removes value: 'selected'
Open up: 00-dom-codealong
Key Objective
Type of Exercise
Location
Timing
6 mins |
|
Key Objective
Type of Exercise
Location
Timing
6 mins |
|
document.createElement()
document.createElement()
textContent
, innerHTML
, or src
.document.createElement()
textContent
, innerHTML
, or src
.appendChild()
createElement()
const item1 = document.createElemement('li');
const item2 = document.createElemement('li');
innerHTML
item1.innerHTML = '<strong>Tea</strong>'
item2.innerHTML = '<em>Coffee</em>'
item3.innerHTML = 'Just normal text'
appendChild()
parent.appendChild(child)
const list = document.querySelector('ul'); // selects ul element
list.appendChild(item1); // adds item1 li to list ul
list.appendChild(item2); // adds item2 li to list ul
Open up: 03-create-append-codealong
Type of Exercise
Location
Timing
Until 20:45 |
|
(Lesson #08)