HTML DOM Manipulation
- Created: March 7, 2024 7:33 AM
- Categories: Reference
- Status:Finished
The only tags you need to know
| HTML Element | Description | 
|---|---|
| <!DOCTYPE html> | Tells the browser it is an HTML document, using the most recent version of HTML. | 
| <html> | Creates the "root" of the document. Everything other than the <!DOCTYPE html> goes inside of the <html></html> tags. | 
| <head> | Contains extra information for the browser that doesn't appear on the page itself. | 
| <title> | The title of the page, which will appear in the browser tab and in search results. | 
| <style> | Used for adding CSS to your page. The <style> allows you to write CSS directly within that HTML file. | 
| <link> | Used to link to a separate (external) CSS file. The <link> requires an href attribute that points to the CSS file, and a rel="stylesheet". | 
| <body> | Any content inside the body is the content that will be on the page itself. | 
| <h1> ... <h6> | Heading levels. These create hierarchy within your page. Think of them as creating a table of contents. | 
| <p> | Paragraph. | 
| <strong> | Strong importance (bold by default). This is an inline element, used inside of paragraphs and headings to put more importance on some of the text. | 
| <em> | Emphasis (italic by default). This is an inline element, used inside of paragraphs and headings to put emphasis on certain words within the text. | 
| <a> | Anchor. Used is to create links - think of it as anchoring to another location. This is an inline element, and can be used inside of paragraphs and headings. | 
| <ul> & <ol> | Unordered & ordered lists. | 
| <li> | List item. Used inside of <ul> and <ol> elements. | 
| <span> | Similar to <strong> and <em> but with no default styling and no semantic meaning. You would use CSS to style it how you want. | 
| <img> | An image. Must have an alt attribute, which describes the image. | 
| <header> | Denotes a heading within the document. Often used for the logo and navigation area on a page. | 
| <main> | The main content of your page (only one per page). | 
| <footer> | Denotes a footer within a document. Like <header>, it is often used as the primary footer for an entire page. | 
| <nav> | Used for major navigational elements (not all links, or lists of links must be in a nav). | 
| <article> | A piece of content on your page that can stand on it's own. | 
| <section> | A section of content. | 
| <div> | A division (or box) - no semantic meaning. These are used to organize your content, generally so you can style layouts with CSS. | 
Getting Started with HTML, and JavaScript
Creating HTML File
Start by creating a new index.html HTML file.
html<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Website Title</title>
    <!-- Linking CSS file -->
    <link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
    <!-- Your HTML content goes here -->
    <!-- Linking JavaScript file -->
    <script src="script.js"></script>
</body>
</html>
DOM
The Document Object Model (DOM) is a tree-like representation of an HTML document. JavaScript can be used to manipulate the DOM to change the appearance, structure, and content of a web page.
Select an element by its id attribute:
jsxconst element = document.getElementById('element-id');
Select all elements with a specific class:
jsxconst elements = document.getElementsByClassName('class-name');
Select all elements with a specific tag:
jsxconst elements = document.getElementsByTagName('tag-name');
Select the first element that matches a CSS selector:
jsxconst element = document.querySelector('.class-name');
Select all elements that match a CSS selector:
jsxconst elements = document.querySelectorAll('.class-name');
Changing Content
Change the text content of an element:
jsxelement.textContent = 'New text content';
Change the HTML content of an element:
jsxelement.innerHTML = '<strong>New HTML content</strong>';
Changing Attributes
Set an attribute:
jsxelement.setAttribute('attribute-name', 'value');
Get an attribute:
jsxconst attributeValue = element.getAttribute('attribute-name');
Remove an attribute:
jsxelement.removeAttribute('attribute-name');
Changing Styles
Change an element's style:
jsxelement.style.propertyName = 'value';
```  <br/>
Add or remove a class:
```jsx
element.classList.add('class-name');
element.classList.remove('class-name');
Toggle a class:
jsxelement.classList.toggle('class-name');
Check if an element has a class:
jsxconst hasClass = element.classList.contains('class-name');
Create a new element:
jsxconst newElement = document.createElement('tag-name');
Add a new child element:
jsxparentElement.appendChild(newElement);
Insert a new element before an existing one:
jsxparentElement.insertBefore(newElement, existingElement);
Remove a child element:
jsxparentElement.removeChild(childElement);
Replace an existing child element:
jsxparentElement.replaceChild(newElement, existingElement);
addEventListener
Attach an event listener to an element:
jsxelement.addEventListener('event-name', eventHandlerFunction);
removeEventListener
Remove an event listener from an element:
jsxelement.removeEventListener('event-name', eventHandlerFunction);
Event Object
The event object provides information about the event:
jsxfunction eventHandlerFunction(event) {
  console.log(event.type); // event name
  console.log(event.target); // element that triggered the event
  // ... other properties and methods
}
Prevent Default Behavior
Cancel the default behavior of an event:
jsxfunction eventHandlerFunction(event) {
event.preventDefault();
}
Stop Event Propagation
Stop the event from bubbling up the DOM tree:
jsxfunction eventHandlerFunction(event) {
  event.stopPropagation();
}
Access an element's parent node:
jsxconst parent = element.parentNode;
Access all child nodes of an element:
jsxconst children = element.childNodes;
Access the first and last child nodes of an element:
jsxconst firstChild = element.firstChild;
const lastChild = element.lastChild;
Access the next and previous sibling nodes of an element:
jsxconst nextSibling = element.nextSibling;
const previousSibling = element.previousSibling;
Find the closest ancestor that matches a CSS selector:
jsxconst closestAncestor = element.closest('selector');
Check if an element matches a CSS selector:
jsxconst isMatch = element.matches('selector');

