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’
What is the textContent property of element objects?
– allows you to access the text content in the element
How do you update the text within an element using JavaScript?
– el.textContent = ‘new text’ or other variable value
Is the event parameter of an event listener callback always useful?
– not always
Would this assignment be simpler or more complicated if we didn’t use a variable to keep track of the number of clicks?
– more complicated
Why is storing information about a program in variables better than only storing it in the DOM?
– quicker, easier to manipulate
What event is fired when a user places their cursor in a form control?
– focus
What event is fired when a user’s cursor leaves a form control?
– blur
What event is fired as a user changes the value of a form control?
– change
What event is fired when a user clicks the “submit” button within a form?
– submit event
What does the event.preventDefault() method do?
- prevent the browser from automatically reloading the page with the form’s values in the URL.
- prevent refresh of page & submission to URL
What does submitting a form without event.preventDefault() do?
– allows refresh of page and submission to URL
What property of a form element object contains all of the form’s controls?
– HTMLFormElement
– nodeList = HTMLFormElement.elements
The HTMLFormElement property elements returns an HTMLFormControlsCollection listing all the form controls contained in the element
What property of a form control object gets and sets its value?
– value
What is one risk of writing a lot of code without checking to see if it works so far?
- many errors, no context
- - loss of specificity