The DOM is a visual layout of the document, this visual highlights the hierarchical relationships with the elements. The DOM allows for use of pop ups, adding and deleting content, editing existing text and identifying the contents of any element.
The Query Selector
query.Selector is a method used to find the content of an element:
constheroImg=document.query.Selector('img')// This will search through the doc and select the first occurrence of "img"
The Query Selector can be used to select many different types of elements and tags:
// using a "." will select all items in a class:letlist1=document.query.Selector('.list1')// using "[]" allows the user to select one element from a list:letlistItem2=document.query.Selector(list1[1])// using "getElementById" selects items based on their id:letfirstHeading=document.getElementById('heading1')
Changing Page Content:
// using the method "innerText" and "+=" allows the user to add content to a single tag:letfirstHeading=document.querySelector('');console.log(firstHeading.innerText);firstHeading.innerText+='thisisthenewheading';// the user can change the text in several tags at once using the "forEach" method:letparagraphs=document.querySelector('');paragraphs.forEach(paragraphs=>{paragraphs.innerText+='newtext'});
Setting Attributes
Using "getAttribute" the user can add attributes to a tag of their choice:
// this example is how to get an attribute to an existing tag:letlink1=document.querySelector('a');console.log(link.getAttribute('href'));// this is an example of how to set an attribute:link.setAttribute('href',https://www.com')
Editing CSS Through the DOM
"setAttribute" is also used to edit existing css:
title.setAttributes('style','margin:50px','border:solidblack')// the user can also add css to existing css:console.log(title.style);title.style.margin='50px';// the user can use console.log to display the value of a css key word:console.log(title.stles.color)
Adding and Removing Classes
using "content" and "classlist" the user can change, add or remove a class:
// display the current classletcontent=document.querySelector('p');console.log(content.classlist);// Adding, using ".add" adds a class namecontent.classList.add('error');// Removingm using ".remove takes a class name awaycontent.classList.remove('error');
Parents, Children and siblings
This is a node tree, this visual represents the relationship between the tags
Elements with the same parent elements are called siblings, elements that come from other elements are known as children
Using the parent-child relationship the user can use "Array.from" and ".children" to apply a class to multiple elements:
the user can use ".remove" and ".addEventListener" to add and remove elements:
// delete elementconstul=document.querySelector('ul');ul.remove();// add method to elementconstbutton=document.querySelector('button');button.addEventListener('click',(=>{ul.innerHTML+='<li>somethingnew</li>';})
Conclusion
The DOM allows for a wide expansion of the user's toolkit. It allows change for HTMl and CSS from java. The DOM could very helpful for revising a web page in progress.
Top comments (0)