Quiz
- Did you have to make the reading quite so long? (I really enjoyed it, though!)
I'm glad you enjoyed it. Yes, it was long. I aim for about 12-15 pages, and this was 19. Dynamically created elements is now and sometimes confusing, and delegated handlers go with dynamic elements, and they are complicated, so there was a lot to say. Bootstrap will be shorter.
- also kind of forget what does <input> </input> and its type and value do
Most of our form inputs are
input
, distinguished by its type:- type=text is for stuff like name, address, city...
- type=password is for passwords (dots instead of characters)
- type=date often gives you a date picker
- type=radio is for a set of mutually exclusive options, like small, medium or large
- type=checkbox is for a set of options, like pizza toppings
- etc.
The value is the value that the user types in or chooses.
- can you explain the val method? what information does it act on?
The jQuery
.val()
method, given a single form input, returns the value that the user typed in (or maybe that is already there, as with radio buttons). - can you explain what you mean by the event.target is similar to this? do we have to use arrow functions to preserve its value?
Oh, I didn't mean that
event
was a special keyword likethis
. Theevent
variable is a normal variable (the parameter of our event handler) and theevent.target
stores the DOM element that the event happened to.For example
event.target
in a click handler is the thing that got clicked on. - I am confused about question 3, could you explain what all the answers do?
Sure. Here it is:
If you've created two DOM elements, p and c, you can make p be the parent of c by A. $(p).parent(c) B. $(p).child(c) C. $(p).append(c) D. $(p).attach(c)
None of those methods exists except
.append()
, which we used in the reading to theinput
element a child of thelabel
element. - Can you explain question 4
Sure. Here's the question:
to create a div with class foo, you can use (check all that apply) A. $('div').attr('class','foo'); B. $('<div>').attr('class','foo'); C. $('<div>', {'class':'foo'}); D. $('div').class('foo');
Only B and C create new elements. They use different but equivalent ways to set an attribute.
- how does CheckList class in CoffeeRun do the checking again?
Here's the constructor:
constructor(selector) { if (!selector) { throw new Error('No selector provided'); } // find the container element and store it for later this.$element = $(selector); if(this.$element.length == 0) { throw new Error(`No element was found for selector ${selector}`) } if(this.$element.length > 1) { throw new Error(`Too many elements were found for selector ${selector}`); } }
It selects the container element and then checks that exactly one element was selected by looking at
$element.length
. Remember that the dollar sign is allowed in variable names, just like an underscore. So, the object just has an instance variable named$element
, which is the thing that it will append checklist items to.