JS dom creation and event delegation Flashcards
Does the document.createElement() method insert a new element into the page?
no it inserts in the DOM tree and needs to be appended to the page later
How do you add an element as a child to another element?
parent.appendChild(child)
What do you pass as the arguments to the element.setAttribute() method?
the attribute and the value
ex) attributes always stored as strings
What steps do you need to take in order to insert a new element into the page?
createElement method, store new element in variable, query for parent element and call appendChild with new element as argument.
What is the textContent property of an element object for?
to change/set the text content (get or set)
Name two ways to set the class attribute of a DOM element.
className or setAttribute
What are two advantages of defining a function to do create something (like the work of creating a DOM tree)?
you can reuse when more stuff is added later, easier to test….can describe code effectively
What is the event.target?
reference to the element where the event occurs-where delegation occurs
Why is it possible to listen for events on one element that actually happen its descendent elements?
event bubbling
What DOM element property tells you what type of element it is?
event.target.tagName
What does the element.closest() method take as its argument and what does it return?
it takes a string with a selector and returns the closest ancestor element that matches that string
How can you remove an element from the DOM?
element.remove method
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?
event.target.tagName needs to have a condition