INFO 253A: Front-end Web Architecture
Kay Ashaolu
<!DOCTYPE html>
<html>
<head>
<title>My Page</title>
</head>
<body>
<div id="main">
<h1 class="header">Welcome</h1>
<p>Hello, World!</p>
</div>
</body>
</html>
console.log(window.document);
console.dir(document);
The document object has properties and methods to interact with the DOM
Examples:
document.body
document.title
document.URL
Methods to select elements, create elements, and more
document.getElementById(id)
to select an elementid
<h1 id="app-title">Shopping List</h1>
const title = document.getElementById('app-title');
console.log(title.innerText); // Outputs: Shopping List
title.innerText = 'My Shopping List';
title.innerHTML = '<em>My Shopping List</em>';
document.querySelector(selector)
const header = document.querySelector('h1');
const mainDiv = document.querySelector('#main');
const items = document.querySelector('.items');
document.querySelectorAll(selector)
const listItems = document.querySelectorAll('.item');
listItems.forEach((item) => {
console.log(item.innerText);
});
const child = document.querySelector('.child');
const parent = child.parentElement;
const parent = document.querySelector('.parent');
const children = parent.children;
console.log(children); // HTMLCollection of child elements
const item = document.querySelector('.item');
const nextItem = item.nextElementSibling;
const item = document.querySelector('.item');
const prevItem = item.previousElementSibling;
const newDiv = document.createElement('div');
newDiv.id = 'new-id';
newDiv.className = 'new-class';
newDiv.textContent = 'Hello World';
document.body.appendChild(newDiv);
innerText
const textNode = document.createTextNode('Hello World');
newDiv.appendChild(textNode);
insertAdjacentElement(position, element)
Positions:
'beforebegin'
'afterbegin'
'beforeend'
'afterend'
const target = document.querySelector('.target');
target.insertAdjacentElement('beforebegin', newDiv);
replaceWith(newElement)
const oldItem = document.querySelector('.old-item');
const newItem = document.createElement('div');
newItem.textContent = 'New Item';
oldItem.replaceWith(newItem);
parent.replaceChild(newChild, oldChild)
const parent = document.querySelector('.parent');
parent.replaceChild(newItem, oldItem);
remove()
method const item = document.querySelector('.item');
item.remove();
removeChild()
method const list = document.querySelector('ul');
const item = document.querySelector('li');
list.removeChild(item);
const link = document.querySelector('a');
const href = link.getAttribute('href');
link.setAttribute('href', 'https://www.example.com');
link.removeAttribute('target');
classList
property
element.classList.add('active');
element.classList.remove('active');
element.classList.toggle('active');
style
property element.style.color = 'red';
element.style.backgroundColor = '#f4f4f4';
const parent = document.querySelector('.parent');
const nodes = parent.childNodes; // Includes text and comment nodes
Use properties like:
firstChild
lastChild
nextSibling
previousSibling
Includes text and comment nodes, not just elements
Minimize Reflows and Repaints
Use Document Fragments
const fragment = document.createDocumentFragment();
// Add elements to fragment
document.body.appendChild(fragment);
Avoid using innerHTML
when possible
Use Event Delegation
Central to Client-Side Scripting
Interacts with APIs and Frameworks
element.addEventListener('eventType', handlerFunction);
document.getElementById('btn').addEventListener('click', handleClick);
target
: element that triggered the event.type
: type of the event.timestamp
: time at which the event occurred.click
dblclick
mouseover
/ mouseout
mousedown
/ mouseup
element.addEventListener('mouseover', function(event) {
event.target.style.backgroundColor = 'yellow';
});
keydown
: when a key is pressed down.keyup
: when a key is released.keypress
: when a character is generated.event.key
: value of the key pressed.event.code
: physical key on the keyboard.input
event fires when value changes. inputField.addEventListener('input', function(event) {
console.log('Current value:', event.target.value);
});
event.preventDefault()
to stop default behavior.FormData
object. form.addEventListener('submit', function(event) {
event.preventDefault();
const data = new FormData(event.target);
console.log('Form data:', data.get('username'));
});
event.target
to identify the clicked element. list.addEventListener('click', function(event) {
if (event.target.matches('li')) {
event.target.classList.toggle('selected');
}
});
window
object represents the browser window.load
: when the page has fully loaded.resize
: when the window is resized.scroll
: when the document is scrolled. window.addEventListener('resize', function() {
console.log('Window resized to', window.innerWidth, 'x', window.innerHeight);
});