DOM Flashcards
What is a DOM?
DOM is called as Document Object Model. Using DOM we can access the web elements. I mean the HTML elements can be accessed. It iwll have a DOM tree internally.
DOM is not part of the JS. It is web APIs inerface provided by the browser.
In the HTML document, we have p tag. How to read and set the value using DOM?
<div>
<p>
</p></div>
Dcoument.querySelector(‘.message’).TextContent.
In the HTML document, we have a input tag. How to read and set the value using DOM?
Document.querySelector(‘.message’).value
In HTML, when a button is clicked, I want to change the number in the input field.
use event listensers.
Document.querySelector(‘.button’).addEventListener(‘click’, function(){
Document.querySelector(‘.message’).value = 5;
}
In HTML, we need to change the color of body using DOM?
Document.querySelector(‘.body’).style.backgroundColor = white;
There is a div tag and I have 3 classes to that div. How do I add / remove classes?
const d = Document.querySelector(‘.divClass’).
d.classList.remove() or .add()
How to listen to an event when a keyboard key is pressed?
document.addEventListener(‘keyDown | keyUp | keyPress’ function(e){
console.log(e.key)
}
How to get/select IDs in theHTML docment?
document.getElementById(idnamewithout#)
How to get/select classes in theHTML docment?
document.getElementByClass(idnamewithout.)
How to get/selects the tags using DOM?
document.getElementsByTagName()
How to get entire HTML page in DOM?
document.documentElement
How to get the head section of a HTML page using DOM?
document.head
How to get the body section of a HTML page using DOM?
document.body
How to create the below HTML in DOM:
<div class=”cookie”></div>
const message = document.createElement(‘div’);
message. classList.Add(‘cookie’);
messgae. textContent = “we dont care”;
message. innerHTML =
const. header = document.querySelector(‘header’);
header. prepend(message)
we also have append
we can either append or prepend
The below code needs to be removed when the button is clicked. How can we do that in DOM?
<div class=”cookie”>
We dont care!
<button class=”btn”> Click here! <button>
</div>
const message = document.querySelector(‘.cookie’);
document.querySelector(‘.btn’).addEventListener(‘click’, function(){
message.remove();
});