From the course: Introduction to Web APIs

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Use the response data in HTML

Use the response data in HTML - REST Tutorial

From the course: Introduction to Web APIs

Use the response data in HTML

- [Instructor] Now let's use the data to display the randomly chosen gif on our page. We want to create an image element. So I'm going to create a variable and I'll call this gifImg. To create an image element, I'm going to do document.createElement. And the element I want to create is an image element. IMG. Now we need to set the source attribute for this image. So gifImg.setAttribute. The attribute we want to set is the source attribute, src. And that's going to be equal to the Gif URL that we got. Okay, now let's put this image onto our page. So document.body.appendChild. And the element that we want to put on our page is our Gif image, gifImg. Okay, let's save this, go back into the browser and see our random gif. We'll refresh the page. Hey! Now that's a squirrel gif. And if you keep refreshing the page, you'll see a different randomly chosen squirrel gif. I could do this all day.

Contents