DOM Flashcards

1
Q

What is a DOM?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In the HTML document, we have p tag. How to read and set the value using DOM?

<div>
<p>
</p></div>

A

Dcoument.querySelector(‘.message’).TextContent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

In the HTML document, we have a input tag. How to read and set the value using DOM?

A

Document.querySelector(‘.message’).value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In HTML, when a button is clicked, I want to change the number in the input field.

A

use event listensers.

Document.querySelector(‘.button’).addEventListener(‘click’, function(){
Document.querySelector(‘.message’).value = 5;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

In HTML, we need to change the color of body using DOM?

A

Document.querySelector(‘.body’).style.backgroundColor = white;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

There is a div tag and I have 3 classes to that div. How do I add / remove classes?

A

const d = Document.querySelector(‘.divClass’).

d.classList.remove() or .add()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to listen to an event when a keyboard key is pressed?

A

document.addEventListener(‘keyDown | keyUp | keyPress’ function(e){
console.log(e.key)
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How to get/select IDs in theHTML docment?

A

document.getElementById(idnamewithout#)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How to get/select classes in theHTML docment?

A

document.getElementByClass(idnamewithout.)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How to get/selects the tags using DOM?

A

document.getElementsByTagName()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How to get entire HTML page in DOM?

A

document.documentElement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How to get the head section of a HTML page using DOM?

A

document.head

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How to get the body section of a HTML page using DOM?

A

document.body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to create the below HTML in DOM:

&ltdiv class=”cookie”>&lt/div&gt

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

The below code needs to be removed when the button is clicked. How can we do that in DOM?

&ltdiv class=”cookie”&gt
We dont care!
&ltbutton class=”btn”&gt Click here! &ltbutton&gt
&lt/div&gt

A

const message = document.querySelector(‘.cookie’);

document.querySelector(‘.btn’).addEventListener(‘click’, function(){
message.remove();
});

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are event handlers?

A

Event handlers are the JavaScript code that invokes a specific piece of code when a particular action happens on an HTML element.

17
Q

How do you add event listeners?

A

document.querySelector(‘.h1’).AddEventListener(‘Click’, function(event){
});

Old school way of doing:

h1.onmouseenter = function(event)…

18
Q

The event should happen only once.. how?

A

document.querySelector(‘.h1’).RemoveEventListener(‘Click’, function(event){
});

19
Q

What is capturing and bubbling phase in DOM?

A

Consider below example:

  <p> hey!  <a> body->section->p->. This is called capturing.

then again it will traverse from below to up. This is called bubbling.

Consider navigation link.

It will have parent nav then menus as nav–links. When we add event listerner to nav–links, then the event listener will also be called for parents DURING BUBBLING phase.

we can stop this navigation by calling e.stopPropogation

We can also enforce during capturing phase using setEventListner(‘click’, function(){

}, true);</a></p>

20
Q

What is event delegation in JS?

A

Event Delegation is basically a pattern to handle events efficiently. Instead of adding an event listener to each and every similar element, we can add an event listener to a parent element and call an event on a particular target using the .target property of the event object.

example, in the navigation bar, instead of listening to the each navigation menu we can listen to we main parent. using e.target we can find the target element and take the action.