Quiz
- What's the difference between a property and an attribute?
Good question. The words are very similar in meaning (in an ordinary dictionary) and are used in very similar ways in coding.
Both are ways to talk about parts, facets, or aspects of things (more synonyms).
In this course, a
property
is a part of an object/dictionary. Like this:An
attribute
is a part of an HTML element (tag). Like this: - Could you explain more about how "data-" attributes are used in code? data-role="trigger" and data-image-url="imgs/otter1.jpg" seem to have been used in different ways.
Sure. The
data-
attributes are things that we as developers can add information to the HTML for our own purposes.Beyond that, we can't say much, any more than we can say what, say, variables are used for: they hold information.
In Ottergram, we use the following
data-
attributes:- the
data-role
tells us what role the element plays. We have triggers (the clickable thumbnails) and also a target and a title. The latter two are modified by the JS. - the
data-image-url
is what value to change thetarget
to - the
data-image-title
is what value to change thetitle
to
- the
- How does the "each" method work? What is "elt"?
elt is a common abbreviation for the word element, which is a member of a set, array or list. Or an HTML element.
See MDN Array.map
.each
is a lot like.forEach
which is similar to.map
. The difference is thatmap
andforEach
work on regular JS arrays, but.each
works on jQuery wrapped sets.map
executes the callback (the function argument) for each element and collects return values in a new array.forEach
executes the callback for each element but doesn't collect return values..each
executes the callback for each element and doesn't collect return values.
The callback function for map/forEach is
function (elt, index, array) {}
.For
each
, the callback function isfunction (index, elt){}
- Can we talk more about the preventDefault method? Is it primarily for hyperlinks/are there other ways to make a button that visually looks like a hyperlink but isn't one?
Yeah, it's used for hyperlinks a lot. Can also be used for form submission buttons, but we haven't gotten to those yet.
-
I was actually very confused by the reading starting from "data attributes" and all the way to "attaching the event handlers". I think the reading was rooted in examples that I couldn't always follow. I guess more specifically, could we talk about the implementation process in more detail? For example, what is happening in these sections of code:
function setDetails(imageUrl, titleText) { $(detailImage).attr('src', imageUrl); $(detailTitle).text(titleText); } imageFromThumb(thumbLinks[0]); titleFromThumb(thumbLinks[0]); imageFromThumb(thumbLinks[1]); titleFromThumb(thumbLinks[1]); function setDetailsFromThumb(thumbnail) { setDetails(imageFromThumb(thumbnail), titleFromThumb(thumbnail)); }
I'm sorry the reading was confusing. I can try to improve it. Please help me do so.
Let's look at those examples using ottergram
- Could you please go over the ottergram JavaScript again? Thank you so much!
Sure! I'll take as much time as people need.
- I don't really have any. Can't wait to code it myself!
Great! Let's do it!