The Document Object Model Chapter 5
The Document Object Model Chapter 5
Model
What is DOM?
getElementById()
Accesses any element on the page via its ID attribute • A
fundamental method within the DOM for accessing elements on the
page •
This method will returns single element innerHTML •
The innerHTML is used to get and replace the content of HTML
elements.
DOM Element
Element by class name
Elements by CSS Selectors
Exercise
as children
DOM Node – Tree representation - Revisit
Navigation between nodes
Adding and Deleting Element
DOM Node - Methods
DOM Animation
<p>One</p>
<p>Two</p>
<p>Three</p>
<script>
let paragraphs = document.body.getElementsByTagName("p");
document.body.insertBefore(paragraphs[2], paragraphs[0]);
</script>
Creating nodes
Say we want to write a script that replaces all images (<img> tags) in the document
with the text held in their alt attributes, which specifies an alternative textual
representation of the image.
This involves not only removing the images but adding a new text node
to replace them. Text nodes are created with the document.createTextNode method.
// Given:
<div>
<span id="childSpan">foo bar</span>
</div>
// Replace existing node sp2 with the new span element sp1
parentDiv.replaceChild(sp1, sp2);
// Result:
<div>
<span id="newSpan">new replacement span element.</span>
</div>
Attributes
Some element attributes, such as href for links, can be accessed through a property of
the same name on the element’s DOM object. This is the case for most commonly
used standard attributes.
But HTML allows you to set any attribute you want on nodes. This can be useful
because it allows you to store extra information in a document. If you make up your
own attribute names, though, such attributes will not be present as properties on the
element’s node. Instead, you have to use the getAttribute and setAttribute methods
to work with them.
<p data-classified="secret">The launch code is 00000000.</p>
<p data-classified="unclassified">I have two feet.</p>
<script>
let paras = document.body.getElementsByTagName("p");
for (let para of Array.from(paras)) {
if (para.getAttribute("data-classified") == "secret") {
para.remove();
}
}
</script>
Styling
The querySelectorAll method, which is defined both on the document object and on
element nodes, takes a selector string and returns a NodeList containing all the
elements that it matches.
<p>And if you go chasing
<span class="animal">rabbits</span></p>
<p>And you know you're going to fall</p>
<p>Tell 'em a <span class="character">hookah smoking
<span class="animal">caterpillar</span></span></p>
<p>Has given you the call</p>
<script>
function count(selector) {
return document.querySelectorAll(selector).length;
}
The querySelector method (without the All part) works in a similar way.
This one is useful if you want a specific, single element. It will return only the
first matching element or null when no element matches.
You can use # Hash symbol to select by Id and . Dot for the class similarly the
tagname you can get By it’s Tag .
The following example we will Select all of them.
<p>And if you go chasing
<span class="animal">rabbits</span></p>
<p>And you know you're going to fall</p>
<p>Tell 'em a <span id="character">hookah smoking
<span class="animal">caterpillar</span></span></p>
<p>Has given you the call</p>
<script>
let byclass = document.querySelector(".animal");
let byId = document.querySelector("#character");
let byTagName = document.querySelector("p");
byclass.style.color = '#8DE3BC';
byId.style.background = "#000";
byTagName.style.background = "#000232"
</script>
End