DOM 0206 Flashcards

1
Q

Why do we log things to the console?

A

so u can see what ur code’s doing

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

What is a “model”?

A

a model made of objects representing the document (html)

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

Which “document” is being referred to in the phrase Document Object Model?

A

the document in the browser, an html document

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

What is the word “object” referring to in the phrase Document Object Model?

A

*all objects that make the model

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

What is a DOM Tree?

A

a model of a web page in form of a tree

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

Give two examples of document methods that retrieve a single element from the DOM.

A

getElementById( ) - uses value of id attribute
querySelector( ) - this uses css selector or element name etc, returns first matching element

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

Give one example of a document method that retrieves multiple elements from the DOM at once.

A

getElementByClassName( )
querySelectorALL( )

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

Why might you want to assign the return value of a DOM query to a variable?

A

In situations where you want to use it more than once.

storing it will make performance better later so the query doent’ have to be run again

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

What console method allows you to inspect the properties of a DOM element object?

A

console.dir( )
*is dir directory?

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

Why would a

 tag need to be placed at the bottom of the HTML content instead of at the top?
A

Because JavaScript runs from top to bottom … the html needs to be parsed before we get to our js

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

What does document.querySelector() take as its argument and what does it return?

A

takes a css selector , or others, as an argument. it returns the first element

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

What does document.querySelectorAll() take as its argument and what does it return?

A

elements (and others) and returns all nodes with that name

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

what is the defer keyword?

A

it is legacy style, but it allows for full page to load before creating dom tree, NO MATTER WHERE the script tag is located

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

What is the purpose of events and event handling?

A

To run code under specific circumstances

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

Are all possible parameters required to use a JavaScript method or function?

A

no

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

What method of element objects lets you set up a function to be called when a specific type of event occurs?

A

add event listener method

E g,

Document. queryselector(class).addeventlistener(‘click’, callbackfunction)

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

What is a callback function?

A

function passed into another as an argument

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

What object is passed into an event listener callback when the event fires?

A

the “event” object

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

What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?

A

the specific html element that was interacted with

I guess you could console log it or you could console.dir it

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

What is the difference between these two snippets of code?

snipped A
element.addEventListener(‘click’, handleClick)

Snipped B
element.addEventListener(‘click’, handleClick())

A

the second one will be unfefined
*(review this for as to why)

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

What is the className property of element objects?

A

it allows you to get or update a class name

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

How do you update the CSS class attribute of an element using JavaScript?

A

select element with class name and assign

  • Query selector for getting
  • .className = ‘class’
    I think without the period for the class name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

What is the textContent property of element objects?

A

allows u to access text content in an element

*And change it?

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

How do you update the text within an element using JavaScript?

A

uses textContent property to update it

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

Is the event parameter of an event listener callback always useful?

A

I believe it is not only useful but also always necessary

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

Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?

A

More complicated because we would have to write some of the code more than once, that is each time we would want to use the code that we could otherwise put inside a variable

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

Why is storing information about a program in variables better than only storing it in the DOM?

A

it’s faster to retrieve, and we don’t want to depend on the dom as it’s dynamic and changing

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

this (below) is a getter/setter. how do u convert from one to the other?

className

A

(this sets it)
$hotButton.className = ‘hot-button hot’;

vs

(this gets it)
$hotButton.className;

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

should u use “innterText” method?

A

no, it’s a hacking vulnerability

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

Css.
What does the transform property do?

A

lets you rotate, scale, skew, or translate an element. It modifies the coordinate space of the CSS visual formatting model.

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

Give four examples of CSS transform functions.

A

rotate, scale, skew, or translate

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

what does the space before colon do?:

.pokeball-wrapper:first-child

.pokeball-wrapper :first-child

A

without space: first child among pokeball wrapper children, that is this Pokeball rappers siblings

with space: first child inside the pokeball wrapper, that is this Pokeball rapper’s children

*double check to confirm this

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

how do you select items for manipulation with the dom?

A

Query selector method

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

how do you listen for things on the dom?

A

Add event listener

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

The transition property is shorthand for which four CSS properties?

A

delay, duration, easing, delay

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

do you apply transitions to the class or pseudo class?

A

apply it to the class for full functionality

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

What event is fired when a user places their cursor in a form control?

A

the “focus” event

38
Q

What event is fired when a user’s cursor leaves a form control?

A

*the “blur” event

39
Q

What event is fired as a user changes the value of a form control?

A

the input event

40
Q

What event is fired when a user clicks the “submit” button within a <form>?

A

the “submit” event

41
Q

What does the event.preventDefault() method do?

A

prevents default behavior,

*and in the case today it prevents the page from resetting after submitting a form

42
Q

What does submitting a form without event.preventDefault() do?

A

it would reload the page

43
Q

What property of a form element object contains all of the form’s controls.

A

.elements

44
Q

What property of a form control object gets and sets its value?

A

.value

45
Q

What is one risk of writing a lot of code without checking to see if it works so far?

A

wasting time!

46
Q

What is an advantage of having your console open when writing a JavaScript program?

A

so you can see functionality thru console logs etc.

47
Q

study and explain this code:

function handleFocus(event) {
console.log(‘focus event fired’);
console.log(event.target.name);
}

function handleBlur(event) {
console.log(‘blur event fired’);
console.log(event.target.name);
}

function handleInput(event) {
console.log(event.target.name, event.target.value);
}

var inputUserName = document.querySelector(‘#user-name’);
var inputUserEmail = document.querySelector(‘#user-email’);
var textArea = document.querySelector(‘#user-message’);

inputUserName.addEventListener(‘focus’, handleFocus);
inputUserName.addEventListener(‘blur’, handleBlur);
inputUserName.addEventListener(‘input’, handleInput);

inputUserEmail.addEventListener(‘focus’, handleFocus);
inputUserEmail.addEventListener(‘blur’, handleBlur);
inputUserEmail.addEventListener(‘input’, handleInput);

textArea.addEventListener(‘focus’, handleFocus);
textArea.addEventListener(‘blur’, handleBlur);
textArea.addEventListener(‘input’, handleInput);

A
48
Q

Does the document.createElement() method insert a new element into the page?

A
49
Q

How do you add an element as a child to another element?

A
50
Q

What do you pass as the arguments to the element.setAttribute() method?

A
51
Q

What steps do you need to take in order to insert a new element into the page?

A
52
Q

What is the textContent property of an element object for?

A
53
Q

Name two ways to set the class attribute of a DOM element.

A

*
.setAttribute or
.setClassName?

54
Q

What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?

A
55
Q

Give two examples of media features that you can query in an @media rule.

A

min-width
max-width

56
Q

Which HTML meta tag is used in mobile-responsive web pages?

A

*viewport?

57
Q

What is the event.target?

A

tells you what was interacted with

58
Q

Why is it possible to listen for events on one element that actually happen its descendent elements?

A
59
Q

What DOM element property tells you what type of element it is?

A

tag name

60
Q

What does the element.closest() method take as its argument and what does it return?

A
61
Q

How can you remove an element from the DOM?

A

using the remove( ) method

62
Q

If you wanted to insert new clickable DOM elements into the page using JavaScript, how could you avoid adding an event listener to every new element individually?

A

*add to parent to catch what happens on children

63
Q

What is the event.target?

A
64
Q

What is the affect of setting an element to display: none?

A

it is treated as having disappeared (not being there)

65
Q

What does the element.matches() method take as an argument and what does it return?

A

*class, id, element?

66
Q

How can you retrieve the value of an element’s attribute?

A

the getAttribute( ) method

67
Q

At what steps of the solution would it be helpful to log things to the console?

A

whenever possible, before writing too much more code

68
Q

If you were to add another tab and view to your HTML, but you didn’t use event delegation, how would your JavaScript code be written instead?

A

target the children

69
Q

If you didn’t use a loop to conditionally show or hide the views in the page, how would your JavaScript code be written instead?

A

more conditional statements (e.g. if else, switch etc.)

70
Q

What is a breakpoint in responsive Web design?

A

the point at which a css rule activates based on parameters set

71
Q

What is the advantage of using a percentage (e.g. 50%) width instead of a fixed (e.g. px) width for a “column” class in a responsive layout?

A

it responds to changes in screen size / different screen sizes

72
Q

If you introduce CSS rules for a smaller min-width after the styles for a larger min-width in your style sheet, the CSS rules for the smaller min-width will “win”. Why is that?

A

all else equal, selectors lower in the document are higher in priority.

that is, source order

73
Q

should design begin with mobile or desktop screens?

A

you should perhaps start with mobile because it’s easier to make things for bigger from smaller screens than vice versa

side note, min width is as screen grows, and what u use when u start with designing for mobile. and vice versa.

74
Q

what do these do?

git stash
git stash apply

A

git stash saves a copy and puts it aside

git stash apply brings that previous copy back

75
Q

how do you get code which spills out the window to fit?

A

ctrl + z

76
Q

What is JSON?

A

JavaScript Object Notation (JSON) is an extremely common data interchange format used to send and store information in computer systems.

It is is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa). You’ll come across it quite often,

77
Q

What are serialization and deserialization?

A

Serialization is the process of turning an object in memory into a stream of bytes (1s and 0s) so you can do stuff like store it on disk or send it over the network.

Deserialization is the reverse process: turning a stream of bytes into an object in memory.

78
Q

Why are serialization and deserialization useful?

A

*so it can be stored and sent.

*it’s more efficient because it turns it into a simpler, more generic format?

79
Q

How do you serialize a data structure into a JSON string using JavaScript?

A

use the JSON.stringify(books)

e.g.
var books = [
{
isbn: ‘1234567’,
title: ‘book one’,
author: ‘author one’
},
{
isbn: ‘234567’,
title: ‘book two’,
author: ‘author two’
}
];

JSON.stringify(books)

80
Q

How do you deserialize a JSON string into a data structure using JavaScript?

A

use: JSON.parse( )

call the method using a string of the data structure, e.g.

JSON.parse(‘{ “id”: 567, “name”: “Jon Doe” }’)

81
Q

How do you store data in localStorage?

A

use the setItem method on the localStorage object, e.g.

localStorage.setItem(‘javascript-local-storage’, todosJSON)

82
Q

How do you retrieve data from localStorage?

A

use the getItem method on the localStorage object, e.g.

localStorage.getItem(‘javascript-local-storage’);

83
Q

What data type can localStorage save in the browser?

A

a JSON string

allows you to access a Storage object for the Document’s origin; the stored data is saved across browser sessions.

84
Q

When does the ‘beforeunload’ event fire on the window object?

A

The beforeunload event is fired when the window, the document and its resources are about to be unloaded.

https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event

85
Q

what is event handling?

A

the three steps involved in getting an event to trigger JS code

those are:
1 select element
2 specify event
3 call the code

86
Q

how do you change the text content of a node?

A

e.g.

document.getElementById(‘divA’).textContent = ‘This text is different!’;

87
Q

what does this tag do?

<meta></meta>

*from css-media-queries exercise

mdn doc:
https://developer.mozilla.org/en-US/docs/Web/HTML/Viewport_meta_tag

A

it sets the scale to 100% on mobile devices, not manipulating it to scale up or down as it usually does in the default mode

88
Q

what does this do:
e.target.closest(‘.task-list-item’) ancestor element.

A

it gets the event’s closest ancestor with that class name, in this case it’s task-list-item.

if the event is a ‘click’ on a button element, which is a child of an element with ‘.task-list-item class, the clicking of the button would get that element (class: task-list-item). it could then be removed by using the .remove() method

89
Q

what is this code doing?

var taskList = document.querySelector(‘.task-list’);

taskList.addEventListener(‘click’, taskListCallback);
function taskListCallback(e) {
console.log(‘event target:’, e.target);
console.log(‘event tagname:’, e.target.tagName);
if (e.target && e.target.nodeName === ‘BUTTON’) {
console.log(taskList.closest(‘.task-list-item’));
e.target.closest(‘.task-list-item’).remove();
}
}

OR THE BELOW(THEY ARE DOING THE SAME THING BUT BELOW HAS ONE LINE SEPARATED INTO A VARIABLE

var taskList = document.querySelector(‘.task-list’)

taskList.addEventListener(‘click’, callBackFunction);
function callBackFunction(e) {
console.log(‘event target:’, e.target);
console.log(‘event tagname:’, e.target.tagName);
if (e.target.tagName === ‘BUTTON’) {
var closestAncestor = e.target.closest(‘.task-list-item’);
console.log(closestAncestor);
e.target.closest(‘.task-list-item’).remove();
}
}

https://photos.app.goo.gl/bBR6vCTogWtTbkKu9

A

you know if you succeeded/failed

90
Q

read and explain this dom-creation code

https://photos.app.goo.gl/oLMaEFW8iumAXgFJ8

A

https://photos.app.goo.gl/oLMaEFW8iumAXgFJ8

91
Q

read and explain this javascript-view-swapping code

JS

https://photos.app.goo.gl/UCmjRdCDEtwipxoA8

HTML
https://photos.app.goo.gl/K2LqJybHX2PxfD3P9

A