<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jennifer Jimenez on Medium]]></title>
        <description><![CDATA[Stories by Jennifer Jimenez on Medium]]></description>
        <link>https://medium.com/@jenniferjimenez90?source=rss-275332fb3459------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gZN6gKYlV1gJs7tBAbxErA.jpeg</url>
            <title>Stories by Jennifer Jimenez on Medium</title>
            <link>https://medium.com/@jenniferjimenez90?source=rss-275332fb3459------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 08:49:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jenniferjimenez90/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to fetch data from an API and render it into cards using JavaScript]]></title>
            <link>https://medium.com/@jenniferjimenez90/how-to-fetch-data-from-an-api-and-render-it-into-cards-using-javascript-b0d1f6e22702?source=rss-275332fb3459------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0d1f6e22702</guid>
            <category><![CDATA[rick-and-morty]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[fetch-api]]></category>
            <category><![CDATA[projects]]></category>
            <category><![CDATA[html]]></category>
            <dc:creator><![CDATA[Jennifer Jimenez]]></dc:creator>
            <pubDate>Tue, 26 Apr 2022 02:28:12 GMT</pubDate>
            <atom:updated>2022-04-26T02:28:12.056Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Rick and Morty" src="https://cdn-images-1.medium.com/max/1024/1*Jmm81eWF8ZtqbfN1HYo8qw.png" /></figure><p>Hello and welcome to my first blog post! For my first project, I wanted to create and incorporate something I enjoy. That something is (if you haven’t guessed from the image above) Rick and Morty. Before getting into web development, I have never heard of an API which is why I chose to use one in my first project. I enjoy watching Rick and Morty and find it hilarious so after a quick Google search for the <a href="https://rickandmortyapi.com/">Rick and Morty API</a> I just knew I had to use it.</p><p><strong><em>Now let’s get started:</em></strong></p><p>To start, we should have an index.html to link our JavaScript (index.js) using the script tag.</p><pre>&lt;!DOCTYPE html&gt;<br>  &lt;html lang=&quot;eng&quot;&gt;<br>    &lt;head&gt;<br>      &lt;title&gt;Rick and Morty Character Generator&lt;/title&gt;<br>     &lt;/head&gt;<br>     &lt;body&gt;<br>       &lt;div id=&quot;cards-container&quot;&gt;<br>        &lt;/div&gt; <br>      &lt;script <em>src</em>=&quot;index.js&quot; defer&gt;&lt;/script&gt;<br>     &lt;/body&gt;<br>  &lt;/html&gt;</pre><p>The script tag is at the end of the body and has the ‘defer’ attribute because we want to make sure our HTML is parsed and loaded before our JavaScript. We can also add a “DomContentLoaded” event listener in index.js instead of the defer attribute as well but <strong>always make sure </strong>to use the script tag in your body or else our JavaScript will not work. In the body tag, we have a div tag with an Id “cards-container”. This will come into use when we create our cards.</p><p><strong>Tip:</strong> When dealing with an API, take a look at the documentation. Usually, you will see what data you will be working with firsthand and how to gain access to certain data simply by reading the documentation.</p><p>To fetch an API you have to use the correct syntax. Before working on this project, I was introduced to fetch but had never worked on fetching data in a project. There is another way to fetch data but we’ll save that topic for another time.</p><p>In our index.js, we should have the following:</p><p><strong><em>syntax:</em></strong></p><pre>fetch(&#39;API_URL&#39;)</pre><pre>.then(response =&gt; response.json())</pre><pre>.then(data =&gt; console.log(data));</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*QRn5UUQHcvbZTK9BHlSwXw.png" /></figure><p>After viewing the <a href="https://rickandmortyapi.com/documentation/">Rick and Morty documentation</a>, we learned to get all characters we need to add the “/character” endpoint. The only change I made from the syntax above is instead of using ‘data’, I decided to use ‘characters’. You can name ‘data’ anything you like as long as you stay consistent with the name. In this case, I decided to name it ‘characters’ because that is the exact data we are looking to fetch.</p><p><strong>Tip</strong>: You should console.log data before starting anything else. This is good beginner practice because you will be able to see the APIs data you are going to be working with on the console and figure out exactly how you can get this data. It is also recommended if your API has more properties you could then use dot notation to access it.</p><figure><img alt="you can see data in the console" src="https://cdn-images-1.medium.com/max/1024/1*Q-9loz7u1wTTLyKf_65Bsw.png" /><figcaption>data in the console</figcaption></figure><p>Since we console.log(characters), we can see the APIs data when we open up the console in developer tools on chrome (Ctrl Shift J on Windows or Ctrl Option J on Mac). It has an object named “info” and an array named “results”. What we want is to get all characters from the “results” array. This time, we’ll use dot notation and add ‘.results’ after characters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*ROnmGMWhJ1GkNIGTSyWRjA.png" /></figure><figure><img alt="how it should look on the console" src="https://cdn-images-1.medium.com/max/1024/1*BjSb-5-ldHyn-LD9AU2vlA.png" /><figcaption>using dot notation we access an array of characters.</figcaption></figure><p>Perfect. Now we know the path to get these characters is “characters.results”.</p><p>Before we create our cards, we want to make sure we get the cards-container div from index.html into index.js so that when we fetch the API, it will have a place to render the characters into. In index.js, we have to create a global variable (outside any function) and use DOM’s (Document Object Model) querySelector() method to get the div with the Id ‘cards-container’.</p><pre>const cardsContainer = document.querySelector(&#39;#cards-container&#39;);</pre><p>That brings us to our next step. <strong>Creating a callback function.</strong></p><p>We will create a function named ‘renderCharacters’ with ‘characters’ (data) in its parameter. In this function’s block, we’ll start with characters followed by the forEach() method. For each character, a div card will be created. In this card, it will have the character’s image, name, type of species, and a like button to like the character. We can create new elements by using the DOM’s createElement() method and declare each newly created element into separate variables.</p><pre>function renderCharacters(characters) {<br>  characters.forEach(character =&gt; {<br>    const div = document.createElement(&#39;div&#39;);<br>    const image = document.createElement(&#39;img&#39;);<br>    const name = document.createElement(&#39;h3&#39;);<br>    const species = document.createElement(&#39;h3&#39;);<br>    const like = document.createElement(&#39;button&#39;);<br></pre><p>Next, we add a class to the div, image, and the like button so we will be able to style them using CSS. To do this we use dot notation after the element’s name, add ‘classList’ and assign the name you wish to use for the element.</p><pre> div.classList = &#39;card&#39;<br> image.classList = &#39;card-img&#39;<br> like.classList = &#39;empty&#39;</pre><p>Depending on the API of your choice, it is possible to get data using dot notation. Since the Rick and Morty API does come with the character’s image, name, and species, it makes it easier for us to use dot notation to get this data.</p><pre> image.src = character.image<br> name.innerText = ` Name: ${<em>character</em>.name} `<br> species.innerText = ` Species: ${<em>character</em>.species} `<br> like.textContent = &#39;like&#39;</pre><p>In order to get the character’s image we need to add ‘src’ (short for source) after image then assign it to character.image. Notice how we used innerText and back ticks (``) for name and species elements. This is necessary if we want to display text next to the characters name and species. We use back ticks and interpolation syntax (`${}`) to do this. Otherwise the ‘Name:’ and ‘Species:’ text would not appear. We also give the like button a textContent of ‘like’.</p><p>Lastly, we need to append these elements to our newly created div element and then append that div to our ‘cardsContainer’ global variable we created earlier. This is an <strong>important step</strong>, otherwise our newly created cards will not appear at all. We do this by using the appendChild() method. We individually append image, name, species, and the like button into the div and then append the div into our ‘cardsContainer’ where our cards will now live.</p><pre> div.appendChild(image)<br> div.appendChild(name)<br> div.appendChild(species)<br> div.appendChild(like)<br> cardsContainer.appendChild(div)<br> })<br>};</pre><p><strong>Tip:</strong> Keep in mind, the order you append an element is the order it will appear on your page. In this case, we want the characters image to appear first followed by the name and so on.</p><p>All together, your function should look like this:</p><pre>function renderCharacters(characters) {</pre><pre>  characters.forEach(character =&gt; {</pre><pre>    const div = document.createElement(&#39;div&#39;);<br>    const image = document.createElement(&#39;img&#39;);<br>    const name = document.createElement(&#39;h3&#39;);<br>    const species = document.createElement(&#39;h3&#39;);<br>    const like = document.createElement(&#39;button&#39;);</pre><pre>    div.classList = &#39;card&#39;<br>    image.classList = &#39;card-img&#39;<br>    like.classList = &#39;empty&#39;</pre><pre>    image.src = <em>character</em>.image<br>    name.innerText = `Name: ${<em>character</em>.name}`<br>    species.innerText =`Species: ${<em>character</em>.species}`<br>    like.textContent = &#39;like&#39;</pre><pre>    div.appendChild(image)<br>    div.appendChild(name)<br>    div.appendChild(species)<br>    div.appendChild(like)<br>    cardsContainer.appendChild(div)</pre><pre>  });<br>};</pre><p>Cool, our final step is to go back to fetch and callback renderCharacters into the second ‘.then()’. We add ‘(characters.results)’ in its parameters since we know that is how to access the array of characters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*5Jc9ch94lBiNuladvNLIhQ.png" /></figure><p>Open up index.html in the browser and you should see something like this.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*9bylXDqCvO_s_lbb9KQHGA.gif" /></figure><p>Thats it! You have officially created a card for each character. Now you can use CSS to style and make the cards look how ever you want. Add a border, center them or if you want to be fancy use flexbox. Since we created a class for div card, image and the like button you should be able to set up rules in CSS. <strong><em>You got this!</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0d1f6e22702" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>