w6d2-d5 - javascript/jquery/dom Flashcards

1
Q

How does document.createElement add an object into the page?

A

It doesn’t.

to append to the page, use [some HTMLElement].appendChild(obj)

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

How do we listen for changes on something in the page?

A

EventTarget#addEventListener

Pass in a callback.

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

What are the 3 ways to call $ or jQuery

A
  1. select elements using CSS selectors
  2. pass it HTML code
  3. pass it an HTMLElement: it will create a jQuery object wrapping that one element.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the primary functions of jQuery

A

selection

traversal

inserting and removing elements

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

What’s the jQuery syntax for an eventListener?

A

jQuery#on(eventName, callback)

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

What’s the jQuery syntax to stop listening to an event?

A

jQuery#off(eventName, callback)

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

What property of the Event object tells us what it is currently focused on?

A

currentTarget

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

Does jQuery wait until the page loads before it runs?

A

No. It executes just like regular JS.

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

How do you ensure that the page is loaded before running jQuery code? (3 ways(

A
  1. Put the script tags at the very bottom of the body.
  2. set a window.onload callback.
  3. use ready-style:

$(someFunction)

This will take a function to run and call that function when the DOM is fully loaded

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

What’s the value of event delegation?

A

Rather than install many identical event handlers on many identical elements, we install one handler for a group of elements (the root element) and call the appropriate element when we hear about an event.

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

How do you set up event delegation in jQuery?

A

on.(event, css_selector, callback)

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

What are the properties of the object passed in to $.ajax and what does each do?

A

method: [‘POST’ , ‘GET’ , …]
url: ‘/someUrl’
dataType: ‘json’
data: body of our request (object or string)
success: callback

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

How do you locate an element inside of another element?

A

$el.find(“div”)

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

What does this do:

$el.find(“div”)

?

A

Finds a div inside of a jQuery element $el

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

The error “something is not a constructor” is indicative of what?

A

module.exports was not set within the class definition .js file.

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

How do do prevent a form from submitting when you click a submit button?

A

$form.preventDefault

17
Q

What function in rails lets you handle different Content-types? How would you handle json and html for an index view?

A

respond_to do |format|
format.html { render :index }
format.json { render :index }
end

18
Q

In a try/catch bloc,, how do you stop execution of subsequent code if an error is thrown?

A

Place a ‘return’ within the catch block.

19
Q

How can ajax selection and manipulation be semantically separated?

A

Selection code goes in binds

Manipulation code goes in callbacks.

20
Q

How do you inject an element before another element?

A

$el.prepend(whatever)

21
Q

What does the ‘error’ key within an ajax call do?

A

A callback that runs if a 400-level error is returned.

22
Q

How do you separate concerns of fetching ajax requests and handling responses?

A

Using promises via .then( ) statements.

The promise returns the response. The .then( ) handles the response.

23
Q

If you have a specific error callback in a promise AND no other promises exist in the chain, will a .catch( ) statement placed at the end of the chain run?

A

No. The error callback will run.

24
Q

What are the three states a Promise can exist in?

A

pending
fulfilled
rejected

25
Q

When a Promise has been either fulfilled or rejected, how do we refer to it?

A

settled

26
Q

How many times can a promise succeed OR fail?

A

once

27
Q

pending
fulfilled
rejected

These refer to the state of what type of construct?

A

Promise

28
Q

With jQuery, how do you make an action fire off an event?

A

$el.closet(“form”).trigger(“submit”)

29
Q

What does the bubbling principle mean in respect to events?

A

When an event triggers, if it isn’t stopped via event.stopPropagation( ), the event’s parent will receive the event, up and up, until it finally hits window.

All parents of the element will also receive the event.

30
Q

How do you stop an event from propagating up?

A

event.stopPropagation( )