Tutorial 03
-
Create an HTML document containing a
<div>element. Using JavaScript and thedocument.createElementmethod, create a<p>element. Set its text content to "Hello, World!" using theinnerTextproperty. Finally, append the paragraph to the<div>element using theappendorappendChildmethod.
Solution
<html lang="en"> <head> <script src="script.js" defer></script> </head> <body> <div></div> </body> </html>const divElement = document.querySelector("div") const pElement = document.createElement("p") pElement.innerText = "Hello, World!" divElement.append(p)
-
Create an HTML document with a
<section>element. Using JavaScript and thedocument.createElementmethod, create a<div>element. Define a constant namedmessagecontaining the string"Hello, World!". Using Template Strings, create a<p>element withmessageas its content. Set theinnerHTMLof the<div>to this paragraph. Finally, append the<div>to the<section>element using theappendChildorappendmethod.
Solution
<html lang="en"> <head> <script src="script.js" defer></script> </head> <body> <section></section> </body> </html>const divElement = document.createElement("div") const message = "Hello, World!" const p = `<p>${message}</p>` divElement.innerHTML = p const sectionElement = document.querySelector("section") sectionElement.append(divElement)
-
Create an HTML document with a
<figure>element. Using JavaScript and thedocument.createElementmethod, create an<img>element. Define a constant namedlinkwith the value"https://www.mtroyal.ca/_files/img/mru-logo-152x100.png". Use thesetAttributemethod to set thesrcattribute of the<img>tolink. Finally, append the<img>to the<figure>element using theappendChildorappendmethod.
Solution
<html lang="en"> <head> <script src="script.js" defer></script> </head> <body> <figure></figure> </body> </html>const imgElement = document.createElement("img") const imageLink = "https://www.mtroyal.ca/_files/img/mru-logo-152x100.png" imgElement.setAttribute("src", imageLink) const figureElement = document.querySelector("figure") figureElement.append(imgElement)
-
Create an HTML document with a
<figure>element. Use thefetchAPI to request data fromhttps://randomfox.ca/floofand extract the image URL from the response. Store this URL in a constant. Using Template Strings, create an<img>element with itssrcattribute set to the image URL. Finally, use theinnerHTMLmethod to insert the<img>element into the<figure>element.
Solution
<html lang="en"> <head> <script src="script.js" defer></script> </head> <body> <figure></figure> </body> </html>const figureElement = document.querySelector("figure") const getData = async function() { const res = await fetch("https://randomfox.ca/floof") const data = await res.json() const imageURL = data.image const imgElement = `<img src="${imageURL}" />` figureElement.innerHTML = imgElement } getData() // call the function
-
Repeat exercise 4, but this time fetch data from https://api.thecatapi.com/v1/images/search.
Solution
Same as above, only change the API address in
fetchand the line that creates theimageURLconstant:const imageURL = data[0].urlCode is correct, but the server may block the request due to security reasons.
-
Repeat exercise 4, but this time fetch data from https://random-d.uk/api/v2/random.
Solution
Same as above, only change the API address in
fetchand the line that creates theimageURLconstant:const imageURL = data.urlCode is correct, but the server may block the request due to security reasons.
-
Repeat exercise 4, but this time fetch data from https://random.dog/woof.json.
Solution
Same as above, only change the API address in
fetchand the line that creates theimageURLconstant:const imageURL = data.urlCode is correct, but the server may block the request due to security reasons.
-
Repeat exercise 4, but add a
<button>element to the HTML document with the textLoad Image. When the user clicks the button, fetch data from the API, extract the image URL, and store it in a constant. Using Template Strings, create an<img>element with itssrcattribute set to the image URL. Finally, use theinnerHTMLmethod to insert the<img>element into the<figure>element.
Solution
<html lang="en"> <head> <script src="script.js" defer></script> </head> <body> <figure></figure> <button>Load Image</button> </body> </html>const buttonElement = document.querySelector("button") const figureElement = document.querySelector("figure") const getData = async function() { const res = await fetch("https://randomfox.ca/floof") const data = await res.json() const imageURL = data.image const imgElement = `<img src="${imageURL}" />` figureElement.innerHTML = imgElement } buttonElement.addEventListener("click", getData)
-
Repeat exercise 8, but this time fetch data from https://api.thecatapi.com/v1/images/search.
Solution
Same as above, only change the API address in
fetchand the line that creates theimageURLconstant:const imageURL = data[0].urlCode is correct, but the server may block the request due to security reasons.
-
Repeat exercise 8, but this time fetch data from https://random-d.uk/api/v2/random.
Solution
Same as above, only change the API address in
fetchand the line that creates theimageURLconstant:const imageURL = data.urlCode is correct, but the server may block the request due to security reasons.