Chapter 5: Document Object Model Flashcards

1
Q

What programming concept is the DOM an example of?

A

An API (Application Programming Interface)

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

What type of Nodes exist in the DOM?

A

document
element
Attribute
text

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

Each node is not an object. True or false?

A

False. Each node is an object with its own properties and methods

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

What methods can select an individual element node

A

getElementById()

querySelector()

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

What methods select multiple elements (novelist)?

A

getElemenetsByClassName()
getElementsByTagName()
querySelectorAll()

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

What methods allow you to traverse the DOM?

A

parentNode
previousSibling / nextSibling
firstChild lastChild

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

What methods allow you to access/update text nodes?

A

nodeValue

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

What methods allow you to work with HTML content?

A
innerHTML
textContent
createElement()
createTextNode();
appendChild();
removeChild();
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What methods or properties allow you to access to update attribute nodes?

A

className / id

hasAttribute();
getAttribute();
setAttribute();
removeAttribute();

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

What do the following terms mean?
DOM query
caching the selection
stores a reference

A

DOM query is a method to find elemtns in the dom

caching refers to assigning result of a dom query to variable. Saves resources if we need ro keep working with an element

Reference means that it stores the location of the node (not the actual node)

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

What is a Nodelist

A

A query that returns a collection of Nodes (at least 1. so if it only finds 1 match its still stored in a Nodelist, that’s different from get ing an individual element).

To access an element in a list we use a Array syntax (either [] or item();

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

What determines if a Nodelist is static or live?

A

It depends on the DOM query method

a live novelist is always run fresh and grabs matching elements. These are methods beginning getElementsBy…

A statici nodelist is run once and does not update subsequent times (so won’t pick up changes in the DOm). querySelector methods create static lists

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

Why’s it a good idea to check a novelist has at least 1 results before running the code?

A

It wastes resources otherwise nothing to update

if()elements.length >= 1 {
//do these cool changes
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Why is Array syntax preferred over item method

A

It’s faster

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

What’s the DOM method to select elements by className?

A

getElementsByClassName(‘className’)

e.g. getElementsByClassName(‘hot’)

This method returns a Nodelist

It’s a newer DOM method so some browsers (IE8 -) don’t support it

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

What’s the DOM method to select elements by their tag?

A

getElementsByTagName(‘tagName’)

e.g. getElementsByTagName(‘a’)

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

What DOM method finds the first matching css selector

A

querySelector(‘selector’)

e.g. querySelector(‘li.hot’)

18
Q

What DOM method finds all matching css selctors

A

querySelectorAll(‘selector’)

e.g. querySelector(‘li.cool’)

19
Q

How do we update elements in a novelist?

A

We need to loop through them

for (var = 0; i

20
Q

Is the following valid:

groceriesList.parentNode();

A

No.

When traversing the DOM, using parentNode, previous/next sibling and first/last child, these are properties of an element (They are not a method).

21
Q

What happens if you call next sibling on an element and their isn’t one?

A

If there’s no matching element the result will be null.

22
Q

True or false: Traversing the DOM properties are read only

A

True. They can only be used to select and element, not change them.

23
Q

How do some browsers treat white space between nodes?

A

All but IE treat white space between nodes as an empty text node. White space means spaces and carriage returns.

24
Q

How many nodes does the following markup create:

<li>Six <em>fresh</em> figs</li>

A

6

there’s an li, plus an attribute node for it

Since there’s extra markup in the contents of li that changes how it’s created. There’s two nodes for the li; one contains six the other fig

There’s another node for the em tag which has it’s own node containing fresh

25
Q

What two ways can we work with the contents of elements

A

Navigate to the text node - works best when only text

Work with the containing element - works better when there’s mix of text and child elements

26
Q

How do we access and update a text node using Node value?

A

Select the containing element, then traverse down and use nodeValue

e.g.

var el = document.getElementById(‘two’).firstChild.nodeValue = “Changed”;

27
Q

How can we see the content of a text node?

A

select the text node and use the property nodeValue

28
Q

Why should we avoid innerText

A

Support - Not by Firefox
Obeys CSS
Performance - slower than textContent

29
Q

What possible issue exists with using the textContent property

A

IE8 and lower - No support

30
Q

How can we add or remove HTML content

A

innerHTML property

DOM manipulation methods

31
Q

How can we set the contents of an li with an id of one. We want to update its contents to include <span>Important</span>Out of stock

A

use innerHTML

document.getElementById(‘one’).innerHTML = ‘‘<span>Important!</span> Out of stock”;

32
Q

How does DOM manipulation work

A

We create an element
We then create a text node
We append the tented to the element
We then append the element to the DOM

e.g var newEl = document.createElement('p');
var newElText = document.createTextNode('Buyers beware!');
newEl.appendChild(newElText);
document.getElementById('main').appendChild(newEl);
33
Q

How would you remove an element.

A

Store the desired element in a variable.

var goodbye = document.getElementsByTagName(‘li’)[3];

Navigate to it’s parent.

var gbParent = goodbye.parentNode;

Then use the removeChild method on the parent element using the variable as an argument

gbParent.removeChild(goodbye);

34
Q

What are pros and cons of inner HTML

A

Pros

  • Add a lot of code using less markup than DOM manipulation
  • Can be faster than DOM manipulation
  • SImple way of removing content

Cons

  • never use it to add user content to a page!(XSS risk)
  • Can be difficult to isolate elements in a large DOM fragment
  • Event handlers may not work as intended
35
Q

What are the Pros and Cons of DOM manipulation

A

Pros

  • Suited to changing DOM fragment where oe element has many siblings
  • It does not affect event handlers
  • easily allows script to add items incrementally

Cons

  • If adding a lot, it can be slower than innerHTML
  • More code
36
Q

What is XSS

A

Cross Site Scripting Attacks

Malicious code trying to be passed into your system. Data you don’t have complete control over is untrusted data

37
Q

How do you defend against cross site scripting?

A
  • Validate data + don’t let untrusted users upload html js
  • Also validate data on the server
  • Whend data is sent to a client escape all potentially dangerous characters
  • Only add user generated content to certain parts of template files
  • Don’t create DOM fragments with untrusted HTML. Only added as text once escaped
38
Q

How can you check if an element has an attribute?

How can you set an attribute?

A

hasAttribute takes an attribute name as an argument

var example = document.getElementById('one');
example.hasAttribute('class');

seAttribute takes two arguments, attribute plus value

example.setAttribute(‘class’, ‘complete’);

39
Q

How can you add a class or id

A

use the setAttribute()

use id() or className() method

These will overwrite what’s there Important for classes (won’t add to it if there’s any existing ones)

40
Q

How do you remove an Attribute

A

notALink.removeAttribute(‘href’);