DOM and BOM Flashcards
What does BOM stand for?
Browser Object Model.
What does DOM stand for?
Document Object Model.
What does the BOM provide us?
It provides us with the window object and associated methods.
Name a BOM method.
alert is an example of a BOM method.
How can we access the BOM methods
We can call them either by window.methodName or just methodName (without the window precursor)
How can we examine the DOM in a web-browser.
We can access it in the Elements tab of the developer tools
How can we access the DOM using JavaScript?
We can access it by using document
When should we use document.querySelector(‘selectorName’)
document.querySelector should be used when the selector is more complicated than other targeted selectors.
What is the syntax for accessing a DOM element using querySelector?
document.querySelector(‘selectorName’)
selectors must be prepended with # or . if they are ids or classes (or [] for attributes)
If there are multiple elements available to querySelector which will be retrieved?
Only the first encountered element matching the querySelector will be retrieved.
Can we combine elementNames in querySelector?
Yes, we can combine them in a long string (including # or . as appropriate)
How can we search inside an element for another element using querySelector?
We can create a variable for an initial querySelector for the outside element then use that as
variable.querySelector(‘innerElementName’)
How can we select something from the DOM using an attribute selector?
document.querySelector(‘[data-type=attributeName]’);
What must things must we remember when selecting an object from the DOM using querySelector?
- selection criteria must be in single quotes
- selection criteria must include the CSS indicator for class or id
- selection will include only the first element found that matches
What symbols need to be included when using querySelector to find an attribute?
Square brackets - []
How do we access the classList property for an element in the DOM?
We would need to selected the element in the DOM and save it to a variable.
let paragraph = document.querySelector(‘p’)
paragraph.classList would then access the p classes
Why would we need to access the classList of an element?
We access the classList of an element to change the CSS classes applied to it, thereby changing its style rendering
How do we add a CSS class to an element in the DOM using JavaScript?
We save the element to a variable to access the classList property
then we variableName.classList.add(‘className’)
to add a className to an element
Can you add multiple classes to an element using classList.add?
Yes, but it might not be considered a best practice
When adding and removing classes to an element, do we need a CSS class indicator ( . ) passed in as an argument?
No, we simply need to pass in the class name in quotes as an argument
How would we remove a class from an element in the DOM?
We would save the element to a variable using
variableName = document.querySelector(‘elementName’)
then variableName.classList.remove(‘className’)
Can we add or remove more than one class using
classList.add or classList.remove?
Yes, by separating them with a comma and surrounding each one with quotation marks
Can we check is an element has a certain class assigned to it?
Yes, we use classList.contains to get a Boolean value back
we can then use this in a conditional statement to change state
Name two ways we could toggle the presence of a class on an element in the DOM
- write a conditional statement based on classList.contains
- use classList.toggle