Javascript p2 Flashcards
DOM querying +
What is the DOM?
Document Object Model
- DOM is an API (Application Programming Interface), a UI/means by which programs and scripts talk to each other.
- The DOM states what your script can ask the browser about the current page, and how to tell the browser to update what is being shown.
Why do we log things to the console?
– to get feedback, learn what is where so we can manipulate it
What is a “model”?
– a replication, example, representation
Which “document” is being referred to in the phrase Document Object Model?
– the website in question
What is the word “object” referring to in the phrase Document Object Model?
- the model is made of objects, an object-model of the document
- each object represents a different part of the page loaded in the browser
What is a DOM Tree?
- a model of a web page
- - document nodes, elements nodes, attribute nodes, text nodes
Give two examples of document methods that retrieve a single element from the DOM.
- getElementById(‘id’), uses value of an elements ID attribute, which should be unique on the page
- querySelector(‘css selector’), returns first matching element
Give one example of a document method that retrieves multiple elements from the DOM at once.
- getElementsByClassName(), grabs all elements of a class
- getElementsByTagName(), grabs all elements of a tag name
- querySelectorAll(), grabs all elements of a CSS selector
Why might you want to assign the return value of a DOM query to a variable?
- cache a thing when you need to work with it more than once
- this stores a reference to where the node is, not the element itself but directions to it
What console method allows you to inspect the properties of a DOM element object?
– console.dir()
Why would a script> tag need to be placed at the bottom of the HTML content instead of at the top?
–the browser needs to parse all the elements in the HTML before the JS code can access them
(sneaky:
script js.link defer script
defer will cache the code to run after page loads so link can go in head)
What does document.querySelector() take as its argument and what does it return?
– takes a CSS selector and returns the first matching element
What does document.querySelectorAll() take as its argument and what does it return?
– takes a CSS selector and returns a node list of all elements which use it
node list vs array?
- a NODE LIST is not an ARRAY. it just sort of looks and acts like one
- they are numerically indexed objects that look like arrays
Why do we log things to the console?
– to get feedback, learn what is where so we can manipulate it
What is the purpose of events and event handling?
- to make the site feel more interactive
- - by causing responses to various prompts
Are all possible parameters required to use a JavaScript method or function?
– no. not all methods require all parameters set, some are optional
What method of element objects lets you set up a function to be called when a specific type of event occurs?
– .addEventListener(‘event’, function, optional)
What is a callback function?
– a function we give to another function
A callback function is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action.
What object is passed into an event listener callback when the event fires?
– the event object, passed in by the element
callBackFunction(event) { }
What is the event.target? If you weren’t sure, how would you check? Where could you get more information about it?
- the target property will store the element that was interacted with
- console.log()
What is the difference between these two snippets of code?
1) element.addEventListener(‘click’, handleClick)
2) element.addEventListener(‘click’, handleClick())
1) element.addEventListener(‘click’, handleClick)
- - this one would run handleClick when the click event fires
- - a callback function
2) element.addEventListener(‘click’, handleClick())
- - this will pass the return of handleClick() as the argument, not the function itself
- - this one would run handleClick as the page loads, rather then on event fire
What is the className property of element objects?
– the classes assigned to that element
How do you update the CSS class attribute of an element using JavaScript?
– el.className = ‘all class names to apply’