Dom Manipulation 1
Dom Manipulation 1
Document Object Model (DOM) manipulation is a JavaScript feature that allows developers to change
the structure, style, and content of web pages. It's a key feature in creating dynamic and interactive web
pages.
Add and remove elements: Create new elements or remove existing ones
WHAT IS DOM?
DOM TREE
FIRST STEP TO DOM MANIPULATION IS TO CREATE A SIMPLE HTML AND CSS BOOTSTRAP DOCUMENT.
AFTER CREATING THE HTML FILE, CREATE A JS FILE ON THE SAME FOLDER THAT HAS THE HTML FILE.
IN MY CASE, IT’S “dom.js” FILE. MAKE SURE YOU LINK YOUR SCRIPT FILE TO YOUR HTML FILE USING THE
SCRIPT TAG.
#1. SHOW ALL THE PROPERTIES AND METHODS, HTML COLLECTIONS OF EVERYTHING THAT’S INSIDE
OR ATTACHED TO THE PAGE/DOCUMENT.
console.dir(document);
#2. SHOW THE DOMAIN OF THE DOCUMENT OBJECT.
console.log(document.domain);
*If we say document.domain, you'll see that we get 127.0.0.1 which is just basically my
Localhost, that's the loopback address.
console.log(document.URL);
*It gives us basically the entire page and the entire URL.
*If we say document.head, that'll grab the head element and we're outputting it to the console, and
you can see everything that's in the head.
*and as you could probably guess we can also do document.body which will grab the body for us and
output it.
#7. SHOW ALL ELEMENTS THAT IS IN THE DOM.
console.log(document.all);
*If we look at our index.html file. The <html> tag is the first one on the top. So in the HTML Collection
of 24, <html> tag is indexed at 0.
*We can access any of these by their index. So for instance, we wanted to access the #header-title and
it has an index of 9 so what we could do is..
console.log(document.all[9])
NOTE: Just to let you know that and anything you add into your file here whether it's in the head or body,
it's going to get added to document.all, it's going to get added to that collection.
console.log(document.forms);
*It's basically going to be a collection of all the forms. We only have one so we have it in the zero index.
*Indexes always start at zero just like a regular array. You can get all the different properties of the
form.
console.log(document.all[9])
*We don't have any links so we just have an empty collection or array [ ].
*We could do images as well, so we can say document.images and you can get images but we don't have any so
we're going to get an empty collection.
NOTE: An HTML collection is not an array.
THIS DOCUMENT. METHOD IS NOT “READ-ONLY”. WE CAN ALSO CHANGE OR MODIFY THE OBJECTS
USING THIS METHOD.
console.log(document.title);
document.title = 123;
We have a few different selecting methods we can use to query the DOM. So the first one we're going to
look at is getElementById() selector.
console.log(document.getElementById('header-title'));
headerTitle.textContent = 'Hello';
*In addition to .textContent, we can also use .innerText, and you’ll get the same result. There are few
differences between the two but for the most part, you can use it interchangeably.
*If we look at it through the inspector, you'll notice that the <h3> is actually inside the <h1>. Now it
doesn't change the <h1> to an <h3>, it puts the html inside of that DOM element. That's what
.innerHTML does.
#15. PUT A 3 PIXEL BLACK BOTTOM BORDER ON THE #header-title TEXT.
NOTE: Before accessing and changing a style of an element, you must declare it first in a variable. You
cannot declare the same variable twice.
*What it gives us is an html collection, just like document.all gives us and each one has an
index.
*We can access individual items by using its index.
console.log(items[1]);
*In this case, we are accessing item 2.
items[1].style.fontWeight = 'bold';
items[1].style.backgroundColor = 'yellow';
NOTE: Notice that I used camel cases here (borderBottom, fontWeight etc.) because you can't do
dashes on JS (border-bottom, background-color) even though, it’s the actual CSS property. So
you have to just type it in camel case.
#ASSIGNMENT.