Difference between revisions of "DOM content from lists"
Jump to navigation
Jump to search
Line 4: | Line 4: | ||
*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 | ||
<pre class=usr> | <pre class=usr> | ||
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; | |||
document.obdy.append(div); | |||
} | |||
}); | |||
<pre class=ans> | <pre class=ans> | ||
for(let beatle of ['John', 'Paul', 'George', 'Ringo']){ | for(let beatle of ['John', 'Paul', 'George', 'Ringo']){ |
Revision as of 07:42, 26 August 2021
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.obdy.append(div); } }); <pre class=ans> for(let beatle of ['John', 'Paul', 'George', 'Ringo']){ let div = document.createElement('div'); div.innerHTML = beatle; document.body.append(div); }