Sure. They are very similar, except in the small part of the
code that builds the dynamic structure,
namely addDomStructure
. Let's look at both.
var box1 = $('<input>').attr('type','radio').attr('value','wzly');in quotes but the class here:
let spanItem = $(""<span>"", {class: ""item""}).text(item);can be not in quotes? Is it because the first one uses attr and the second one uses the second argument of $?
Because object literals treat the keys as constants, not variables. Consider:
BTW, there's a cute trick:
the item
argument is the thing we are looking
for. The elt.item
is the value in the dictionary.
Consider the equivalent Python code:
Glad to. It iterates over groceries (a list/array) and for each
element, elt
, it adds a DOM element to the page for that
grocery item.
After having found something, you could look up/down from it for something you want even more.
Well, the template would be just typed into our HTML file, as static text. We then clone from it dynamically. We can take another look at the grocery example.
Scale. I use cloning when the HTML gets "big" but if it's small, I usually just do things directly:
I meant duplicating the clone as a "distractor", because you're right, you don't clone the clone. A few people tripped up on that, unfortunately.
Correct. We are cloning the HTML, not the clone.
Alas, visualizations are hard over zoom, but tomorrow I'll draw some pictures.
For sure; I'll talk about it some more.
If you need to change a specific element, you need to determine how it is different from all the other clones.
Id are not illegal, but I have found that students almost always
get them wrong. They create N clones, all of which have the same ID,
and then they can't figure out why $("#id")
doesn't work.