Manipulating the DOM Flashcards

1
Q

What is the process to add an element as a child of another on a webpage using js?

A

1 Assign an element to a variable
2 Create a new element in memory
3 Add attributes and styles to it
4 Insert it into the DOM. Append it to the parent

1
const container = document.querySelector('#container');
2
const content = document.createElement('div');

3

content. classList.add(‘content’);
content. textContent = ‘This is the glorious text-content!’;

4
container.appendChild(content);

&ltbody>
  &lth1>
    THE TITLE OF YOUR WEBPAGE
  &lt/h1>
  &ltdiv id="container">
  	&ltdiv class="content">
      This is the glorious text-content!
    &lt/div>
  &lt/div>
&lt/body>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create a div in memory?

A

const div = document.createElement(‘div’);

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

How do you add a child node to a parent:

  • at the end?
  • before a specific node?
  • after a specific node?
A

parentNode.appendChild(childNode) appends childNode as the last child of parentNode

parentNode.insertBefore(newNode, referenceNode) inserts newNode into parentNode before referenceNode

parentNode.insertBefore(newNode, referenceNode.nextSibling);

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

How do you remove a child node from a given parent?

A

parentNode.removeChild(child) removes child from parentNode on the DOM
returns a reference to child

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

What does parentNode.appendChild(childNode) do and/or return

A

Appends childNode as the last child of parentNode

Returns A Node that is the appended child (aChild), except when aChild is a DocumentFragment, in which case the empty DocumentFragment is returned.

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

What does parentNode.insertBefore(newNode, referenceNode) do and/or return?

A

Inserts newNode into parentNode before referenceNode

Returns Returns the added child (unless newNode is a DocumentFragment, in which case the empty DocumentFragment is returned).

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

What does parentNode.removeChild(child) do and/or return

A

parentNode.removeChild(child) removes child from parentNode on the DOM a
returns a reference to child

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

Add an inline style using js

  • 1 style
  • multiple styles
  • by setting an attribute
A

const div = document.createElement(‘div’);

div.style.color = 'blue';                                      
// adds the indicated style rule
ADDS and OVERWRITES

div.style.cssText = ‘color: blue; background: white;’;
// adds several style rules
div.style works too
This also SETS the attribute to this value. It DOES NOT ADD. It REPLACES

div.setAttribute(‘style’, ‘color: blue; background: white;’);
// adds several style rules
This also SETS the attribute to this value. It DOES NOT ADD. It REPLACES

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

What does
div.style.background-color
do or return

A

an error

the - attempts to subtract the two values

if the css rule as a -, then you have to use 
camel case: 
backgroundColor 
or
bracket notation:
['background-color']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do you refer to a css rule that uses a -

like background-color? (can use dashes in js)

A
It's automatically converted to camel case: 
backgroundColor 
or you can use
bracket notation:
['background-color']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Change an id name (or change any attribute) (2 ways)

A
div.setAttribute('id', 'theDiv');                              
// if id exists, update it to 'theDiv', else create an id
// with value "theDiv"

or

div.id = “theDiv”

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

What does div.setAttribute(‘id’, ‘theDiv’); do and/or return

A
// if id exists, update it to 'theDiv', else create an id
// with value "theDiv"

returns undefined

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

add,
remove,
and toggle
a css class

A
div.classList.add('classname');                                      
// adds class "new" to your new div
div.classList.remove('classname');                                   
// removes "new" class from div
div.classList.toggle('classname');                                
// if div doesn't have class "active" then add it, or if
// it does, then remove it
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What do these do and/or return

div. classList.add();
div. classList.remove();
div. classList.toggle();

A
div.classList.add('classname');                                      
// adds class "classname" to your new div
no return value
div.classList.remove('classname');                                   
// removes "classname" class from div
// no return value
div.classList.toggle('classname', boolean);                                
// if div doesn't have class "classname" then add it, or if
// it does, then remove it
Returns A boolean value, true or false, indicating whether token is in the list after the call or not.
Can do it based on any Boolean return.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Should you remove a class list or toggle it?

A

Toggle it

It has more functionality?

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

Add text to dom using js

A
div.textContent = 'Hello World!'                               
// creates a text node containing "Hello World!" and
// inserts it in div
17
Q

What does
div.textContent = ‘Hello World!’
do and/or return

A
// creates a text node containing "Hello World!" and
// inserts it in div
18
Q

What does
div.innerHTML
do?
Why should you not use it?

A
div.innerHTML = '<span>Hello World!</span>';                   
// renders the HTML inside div

Easier to hack for some reason.

19
Q

What will the dom look like after this code:

const container = document.querySelector(‘#container’);

const content = document.createElement(‘div’);

content. classList.add(‘content’);
content. textContent = ‘This is the glorious text-content!’;

container.appendChild(content);

A
&ltbody>
  &lth1>
    THE TITLE OF YOUR WEBPAGE
  &lt/h1>
  &ltdiv id="container">
  	&ltdiv class="content">
      This is the glorious text-content!
    &lt/div>
  &lt/div>
&lt/body>
20
Q

When the dom is manipulated, it does not change the HTML

t or f

A

t

21
Q

How can you make sure your JS file is read after the HTML is run (so the DOM items actually exist)
(2 ways)

A
Put the script link at the end
Or
Put it in the head with the defer tag
&lthead>
 &ltscript src="js-file.js" defer>&lt/script>
&lt/head>
22
Q
what is e in:
btn.addEventListener('click', function (e) {
  console.log(e);
});
?
A

The e in that function is an object that references the event itself.
Has a bunch of properties.

23
Q

what is e.target in:
btn.addEventListener(‘click’, function (e) {
console.log(e.target);
});

A

It’s the target of the event. In this case btn. It will log the element and all it’s contents

24
Q
What does:
btn.addEventListener('click', function (e) {
  e.target.style.background = 'blue';
});
do?
A

turns the button background blue

25
Q

add an event

A

target.addEventListener(‘event id’, callback function);

26
Q

make something happen on click (without addEventListener)

2 ways

A

Or onclick inline or in js

// the JavaScript file
const btn = document.querySelector('#btn');
btn.onclick = () => alert("Hello World");
27
Q

Attach event listeners to a group of nodes

A
&ltdiv id="container">
    &ltbutton id="1">Click Me&lt/button>
    &ltbutton id="2">Click Me&lt/button>
    &ltbutton id="3">Click Me&lt/button>
&lt/div>
// buttons is a node list. It looks and acts much like an array.
const buttons = document.querySelectorAll('button');
// we use the .forEach method to iterate through each button
buttons.forEach((button) => {
  // and for each one we add a 'click' listener
  button.addEventListener('click', () => {
    alert(button.id);
  });
});
28
Q

list some event listeners (4) (there are hundreds)

A

click
dblclick
keydown
keyup

29
Q

What is code-key

A

a number representing a specific key