What is the best way to incorporate images from JSON responses into an HTML document?

I successfully managed to populate an HTML table with the json responses. However, I'm encountering an issue when it comes to displaying the images associated with the json variables. I'm only able to display the URL links for the images.

Below is a snippet of the json file:

array (
  'api' => 
  array (
    'results' => 10,
    'fixtures' => 
    array (
      0 => 
      array (
        'fixture_id' => 932017,
        'league_id' => 4633,
        'league' => 
        array (
          'name' => 'Pro League',
          'country' => 'Saudi-Arabia',
          'logo' => 'https://media-3.api-sports.io/football/leagues/307.png',
          'flag' => 'https://media-3.api-sports.io/flags/sa.svg',
        ),
        'event_date' => '2023-01-22T20:30:00+03:00',
        'event_timestamp' => 1674408600,
        'firstHalfStart' => NULL,
        'secondHalfStart' => NULL,
        'round' => 'Regular Season - 14',
        'status' => 'Not Started',
        'statusShort' => 'NS',
        'elapsed' => 0,
        'venue' => 'Mrsool Park',
        'referee' => NULL,
        'homeTeam' => 
        array (
          'team_id' => 2939,
          'team_name' => 'Al-Nassr',
          'logo' => 'https://media.api-sports.io/football/teams/2939.png',
        ),
        'awayTeam' => 
        array (
          'team_id' => 2934,
          'team_name' => 'Al-Ettifaq',
          'logo' => 'https://media.api-sports.io/football/teams/2934.png',
        ),
        'goalsHomeTeam' => NULL,
        'goalsAwayTeam' => NULL,
        'score' => 
        array (
          'halftime' => NULL,
          'fulltime' => NULL,
          'extratime' => NULL,
          'penalty' => NULL,
        ),
      ),

Furthermore, here is the code snippet for importing the json output into the HTML table:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<style>
    th{ 
        color:#fff;
            }
</style>


<table class="table table-striped">
    <tr  class="bg-info">
        <th>Home Team</th>
        <th>Match Date</th>
        <th>Away Team</th>
    </tr>

    <tbody id="myTable">
        
    </tbody>
</table>

<script>
    var myArray = []
    

    $.ajax({
        method:'GET',
        url:'https://api-football-v1.p.rapidapi.com/v2/fixtures/team/2939/next/10?rapidapi-key={API-Key}',
        success:function(response){
            myArray = response
            buildTable(myArray)
            console.log(myArray)
        }
    })



    function buildTable(data){
        var table = document.getElementById('myTable')
        for (var i = 0; i < data.api.fixtures.length; i++){
        const rm = data.api.fixtures[i];

            var row = `<tr>
                       <td>${rm.homeTeam.logo}</td>
                       <td>${rm.event_date}</td>
                       <td>${rm.awayTeam.logo}</td>
                      </tr>`
            table.innerHTML += row


        }
    }

</script>

To address this issue, I am looking to display the actual images associated with homeTeam.logo and awayTeam.logo instead of just their URLs. Any help on this matter would be greatly appreciated. Thank you.

Answer №1

Maybe consider including an img tag in your code. It could be what you're looking for...

var row = `<tr>
    <td><img src="${rm.homeTeam.logo}" /></td>
    <td>${rm.event_date}</td>
    <td><img src="${rm.awayTeam.logo}" /></td>
    </tr>`

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for utilizing variables as the initial value of a JSON Object

Here is a JSON configuration example: { "Users" : { "182723618273612" : 15, "AddedUser" : 1 } } I have generated this field using a JavaScript function, but now I want to change the name of "AddedUser" ...

Looking for a way to efficiently sort through props in Next.js? Struggling with filtering data within props from componentDidMount in Next.js?

I retrieve data into the props of my component using getStaticProps. However, I need to filter this data before utilizing it in the component. Typically, I would do this in componentDidMount, but it appears that the props are populated after componentDidMo ...

Passing component properties using spaces in VueJS is a simple and effective

I am encountering an issue where I want to pass component props from my view, but I am facing a challenge due to the presence of a space in the value. This causes Vue to return the following error: vendor.js:695 [Vue warn]: Error compiling template: - inva ...

Customizing Tabs in Material UI v5 - Give your Tabs a unique look

I am attempting to customize the MuiTabs style by targeting the flexContainer element (.MuiTabs-flexContainer). Could someone please clarify the significance of these ".css-heg063" prefixes in front of the selector? I never noticed them before upgrading my ...

Update the nested radio button to a new value

I have a series of radio button lists generated using ng-repeat. I've managed to capture the selected item successfully. Now, I am attempting to set the default value for the radio buttons. Could someone please provide assistance? Below is my code sni ...

Choose to either push as a single object or as individual items

I have a quick question that I'd like to get some clarity on. Can someone explain the distinction between these two code snippets: export const addToCart = function(product, quantity){ cart.push({product, quantity}); console.log(`${quantity} ...

Is it possible to conceal the text specifically inside a textarea, rather than just hiding the entire textarea itself?

Is it possible to have a button next to the textarea that allows you to hide and unhide the text inside the textarea by clicking on it? ...

Creating JSON Stringer in Android for the specified format

My task involves sending data to a database in a specific format - {"param1":"value1", "param2":"value2", "param3": {"username": "admin", "password": "123"}} I am trying to figure out how to achieve this using JSONStringer. Can you help? This is what I ...

React/JavaScript: Leveraging the power of React spread for simultaneously making two modifications to an array

I need to update the key/value pair 'timestamp' in my array and rename it to 'start'. In addition to adding an 'id' to each item with a value, I would like to change 'timestamp' to 'start'. Can this be achi ...

Using Discord.js to retrieve identical lines from a text file in a database

I've been working on a discord bot that is supposed to send a line from a .txt file to the message author via DM, then delete that specific line from the file. However, it seems to be sending the same line repeatedly. Any suggestions on how to resolve ...

Generate a Monaco Editor within a Vue.js component

Currently, I am integrating Monaco Editor with Vue.js and facing some confusion regarding how Monaco is being instantiated within the Vue component: 1) In my data() method, I have defined an editorEx object to be used for this purpose, like so: data() { ...

Efficiently managing data with Ajax View functionality in asp.net mvc through paging, sorting, and searching

Currently, I am utilizing asp.net mvc 4 along with EF 6 to create a website. The goal is to have a populated table that allows for paging, sorting, and filtering/search on a PartialView using Ajax. The functionality for paging, sorting, and searching is al ...

An automatic selection in my AngularJS drop-down menu

I am currently utilizing AngularJS to develop a basic web application. My goal is to have the values of city A displayed as the default choice in my select dropdown. index.html <body ng-controller="MainCtrl"> <select ng-model="selectedCity" ...

An error is triggered by serializing a TinyBox POST form into an Array

When I implemented the code below, it performed as anticipated: TINY.box.show({url:target, post:$("form[name='currentSearch']").serialize(), width:650, mask:true, close:true, maskid:'boxMask', boxid:'popupBox', openjs:funct ...

Running a JavaScript function on an external site using C#

I'm having trouble downloading an image from an external site using C#. The issue is that the image's source is generated server-side with ajax and isn't accessible from the page's source code. The JavaScript function responsible for ge ...

Creating variables within an AJAX 'success' callback function in JavaScript

$(document).ready(function() { $.ajax({ url: 'objects.php', type:'GET', dataType: 'json', success: function(response) { var variable = [some_arra ...

Error: The 'length' property of the twitter object is not defined and cannot be read

I am trying to fetch data from my Twitter account using PHP and JSON. My PHP code is working fine, but I am facing an error with jQuery. Uncaught TypeError: Cannot read property 'length' of undefined $(document).ready(function() { var dm ...

The then() function in Node.js is triggered before the promise is fully resolved

I'm struggling to get my Promise function working as intended. Here's what I need to accomplish: I am receiving file names from stdout, splitting them into lines, and then copying them. Once the copy operation is complete, I want to initiate oth ...

Swipe JS: tap on the edge to view the next item

Currently utilizing Swipe JS to generate a full-screen image gallery and aiming to incorporate the functionality of clicking on the left or right edge to navigate between the previous and next slides. An attempt was made to create absolutely positioned a ...

Receive notification of alert content when it is displayed

Having an HTML page with the following script: <Script> function Run() { alert("The Content Here"); return true; } </script> <button onclick="Run();">I Want It Now</button> If I were to open this page using firefox or Chrome, and ...