Difference between revisions of "DOM working with data"
Jump to navigation
Jump to search
Line 33: | Line 33: | ||
let tgt = r[42]; | let tgt = r[42]; | ||
document.body.innerHTML = ` | document.body.innerHTML = ` | ||
<div>${tgt.name} | <div>${tgt.name}</div> | ||
<div>${tgt.continent})</div> | |||
<div>${tgt.capital}</div> | <div>${tgt.capital}</div> | ||
<div>population: ${tgt.population}</div> | <div>population: ${tgt.population}</div> | ||
Line 48: | Line 49: | ||
<div>${tgt.capital}</div> | <div>${tgt.capital}</div> | ||
<div>population: ${tgt.population}</div> | <div>population: ${tgt.population}</div> | ||
<img src='${tgt.flag}' style='width:100'/> | |||
`; | `; | ||
}); | }); | ||
</pre> | </pre> | ||
</div> | </div> |
Revision as of 18:30, 21 August 2021
Get the name from the number
- You can obtain data from the server using a fetch call.
- In these examples you will be getting data from /worldl.json this is a list of 195 countries in json format
- The program snippet shows the name of country 42.
- Change it so it shows the name of country 50.
- See if you can find your country.
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[42]; document.body.innerHTML = tgt.name; });
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[50]; document.body.innerHTML = tgt.name; });
Get the details from the number
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[42]; document.body.innerHTML = ` <div>${tgt.name}</div> <div>${tgt.continent})</div> <div>${tgt.capital}</div> <div>population: ${tgt.population}</div> `; });
fetch('/worldl.json') .then((r)=>r.json()) .then((r)=>{ let tgt = r[50]; document.body.innerHTML = ` <div>${tgt.name} (${tgt.continent})</div> <div>${tgt.capital}</div> <div>population: ${tgt.population}</div> <img src='${tgt.flag}' style='width:100'/> `; });