Difference between revisions of "DOM content from lists"
Jump to navigation
Jump to search
Line 1: | Line 1: | ||
<pre id='shellbody' data-qtp='fetch'></pre> | <pre id='shellbody' data-qtp='fetch'></pre> | ||
<div class=qu> | <div class=qu> | ||
beatles.json | |||
{"members": | |||
[ | |||
{"firstName":"Paul","lastName":"McCartney"}, | |||
{"firstName":"John","lastName":"Lennon"}, | |||
{"firstName":"Ringo","lastName":"Starr"}, | |||
{"firstName":"George","lastName":"Harrison"} | |||
] | |||
} | |||
== Show a list of numbers == | == Show a list of numbers == | ||
*When you have data in a list you can use a for loop to generate content | *When you have data in a list you can use a for loop to generate content | ||
Line 10: | Line 20: | ||
let div = document.createElement('div'); | let div = document.createElement('div'); | ||
div.innerHTML = beatle.firstName; | div.innerHTML = beatle.firstName; | ||
document. | document.body.append(div); | ||
} | } | ||
}); | }); | ||
</pre> | </pre> | ||
<pre class=ans> | <pre class=ans> | ||
for(let beatle of | fetch('/beatles.json') | ||
.then((r)=>r.json()) | |||
.then((r)=>{ | |||
for(let beatle of r.members){ | |||
} | let div = document.createElement('div'); | ||
div.innerHTML = `${beatle.firstName} ${beatle.lastName}`; | |||
document.body.append(div); | |||
} | |||
}); | |||
</pre> | </pre> | ||
</div> | </div> |
Revision as of 07:46, 26 August 2021
beatles.json
{"members": [ {"firstName":"Paul","lastName":"McCartney"}, {"firstName":"John","lastName":"Lennon"}, {"firstName":"Ringo","lastName":"Starr"}, {"firstName":"George","lastName":"Harrison"} ] }
Show a list of numbers
- When you have data in a list you can use a for loop to generate content
fetch('/beatles.json') .then((r)=>r.json()) .then((r)=>{ for(let beatle of r.members){ let div = document.createElement('div'); div.innerHTML = beatle.firstName; document.body.append(div); } });
fetch('/beatles.json') .then((r)=>r.json()) .then((r)=>{ for(let beatle of r.members){ let div = document.createElement('div'); div.innerHTML = `${beatle.firstName} ${beatle.lastName}`; document.body.append(div); } });