How to present images and descriptions in a list view on an HTML page using JSON data

I am new to using JavaScript and need help displaying images and descriptions in a list view on an HTML page retrieved from a JSON object.

I have parsed a JSON object and extracted the image URL and description. Now, I need to showcase the Image and Description in a list view on my HTML page, but I'm unsure how to do it. Please assist me in resolving this issue.

Below is the structure of My HTML Page:

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<link href="../css/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../js/jsonData.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $("#submit").click(searchAPI);
    })
</script>

</head>
  <body>
    <input type="text" id="search1" value="search here"  >
    <button id="submit" >submit</button>
    <div id="div1">
    <img id="img"  src="http://hdcomputerwallpaper.com/wp-content/uploads/2013/12/Puppy-images.jpg"  style="width:200px;height:200px;">
    <p id="p2">  </p>
    </div>
    </div>
    </body>
 </html>

This is the content of My js file:

 function searchAPI(){
var searchText= $("#search1").val();
alert(searchText);
$.getJSON('http://api.duckduckgo.com/?q=ashok&format=json&pretty=1&callback=?',function(data){
    for (var i = 0; i < data.RelatedTopics.length;i++) {
        var desc= data.RelatedTopics[i].Text;
        var url= data.RelatedTopics[i].Icon.URL;
    }
});
}

Please guide me on how to arrange and display these Images and Descriptions in a list view as depicted above.

Answer №1

Looks like this solution will do the trick for you.

To make changes in JavaScript, consider the following code snippet:

function findResults(){
var searchText = $("#search1").val();
alert(searchText);
var htmlOutput = "<ul>";
$.getJSON('http://api.duckduckgo.com/?q=ashok&format=json&pretty=1&callback=?', function(data){
    for (var i = 0; i < data.RelatedTopics.length; i++) {
        var description = data.RelatedTopics[i].Text;
        var imageURL = data.RelatedTopics[i].Icon.URL;
        htmlOutput += "<li><img src='" + imageURL + "' style='width:100px;height:100px;display:inline-block'/> <p style='display:inline-block'>" + description + "</p></li>";
    }
htmlOutput += "</ul>";
$('#div1').html(htmlOutput);
});
}

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

Interactive Sidebar Navigation Content

On my website, I have a navigation sidebar that contains links to all of the main site pages. Each page has the same links, except the link to the current page is not included, making it easy to visually identify which page you are on. Currently, I manuall ...

Utilizing media queries in AMP for optimizing mobile website performance

Recently, I delved into the realm of utilizing AMP for my blog. After perusing the documentation and observing examples of its application, I must say that anything enhancing performance is a welcome addition to me. Therefore, I am keen on incorporating i ...

Exploring Angularjs: Retrieving the value of a selected optgroup

When using my application, I need to extract the selected optgroup value from a dropdown menu. Here is the sample code: HTML: <select ng-model='theModel' ng-change="display(theModel)" > <optgroup ng-repeat='group in collectio ...

Axios is repeatedly making GET requests to an incorrect endpoint

I have set up axios to make requests to my express backend hosted on localhost:8081 src/htpp/index.js import axios from 'axios' export default axios.create({ baseURL: 'http://localhost:8081/api/', timeout: 1000, headers: {&apos ...

Dropdown colorization

I want to create a dropdown menu in my black navbar, but the text color becomes blue and the background of the pop-up turns white, which clashes with my black background. I need it to be transparent while using Bootstrap v4.1.1 <nav className="navbar n ...

Issue with AngularJS form not binding to $http request

<form novalidate class="form-horizontal"> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <div class="text-capitalize"> </ ...

Stop Chrome mobile from automatically detecting phone numbers

I encountered a problem with a webpage on Chrome mobile where text content was mistakenly detected as a phone number. Fortunately, I was able to resolve the issue on Safari by including a specific meta tag. <meta name="format-detection" conte ...

Is there a way to slow down the scrolling speed of a div with a fixed background attachment using CSS

I'm experimenting with creating unique parallax effects and I'm wondering if it's possible to make a div with a background image set to background-attachment: fixed scroll at a slower rate than the normal scroll speed, rather than staying co ...

Modify the icon in the header of MaterializeCSS Collapsible when it is opened

I'm struggling to figure out how to change the icon of a toggled collapsible element. I have been reviewing their documentation but am having trouble making it work as intended. $('.collaps_roles_permission').collapsible({ accordion: tr ...

What is the process for executing JavaScript code that is stored as a string?

After making an AJAX call, I receive a random string (constructed dynamically on the server) that contains JavaScript code like: Plugins.add('test', function() { return { html: '<div>test</div&g ...

How can the camera in ThreeJS be shifted horizontally without being affected by its current orientation?

In my ThreeJS setup, the camera is able to move through a 3D world and change its lookAt multiple times while in motion. As a result, camera.getWorldDirection() will always be more or less random. Now, I require the camera to move specifically left, right ...

Encountering a CORS issue for the second consecutive time when making the identical GET request

I encountered an issue with this line of code: let obj = await (await fetch(url)).json(); The first time I execute the function containing this code, everything works smoothly and I receive the desired JSON response. However, upon the second execution in ...

Combine multiple Line objects in THREE.js using GeometryUtils.merge

Is there a way to improve performance by merging multiple Line objects into a single geometry using GeometryUtils.merge? It doesn't seem to work as expected with Line objects. Is it necessary to redefine the concept of a line to achieve this optimizat ...

Sending JSON data two times to the ASP.NET MVC controller via POST request

function onTestComplete(content) { var url = '<%= Url.Action("JsonTest","Organization") %>'; $.post(url, null, function(data) { alert(data["name"]); alert(data["ee"]); }); } <% using (Ajax.BeginForm("JsonTe ...

The custom error page in NextJS is failing to display

In my custom pages/404.ts file, I have coded the following: export default function NotFound() { return <h1>404 - Page Not Found</h1> } Additionally, there is another page that displays a 404 error when the organization is null: import Error ...

Dynamically adjusting the background color of table elements using JavaScript

Snippet code copied from this link In my app, clicking on the Add New Item button dynamically adds rows. Clicking on any number in the table populates it in the corresponding row. Hovering over the 1st row changes its background color to green along with ...

Adjust the div to match the height of the td element

Hey there, can you help me out with this code snippet? <html> <body> <table> <tr> <td> <div class="the-div">h:100%</div> </td> ...

Display a pop-up upon clicking an image or button

My goal is to create a popup that collects emails when someone interacts with an image as if it were a button. In my footer.php file, I have included the following code (with placeholders for privacy reasons): <script> function showMailingPopUp() { ...

Do not decode HTML content within an iframe; instead, extract the data directly from the HTML source

To expedite execution time, we have made the decision to not display the table in the iframe as it is invisible to the client. However, we still need to update the main page table by copying its contents. The approach we are taking is that the iframe shou ...

What is causing the spinner with CSS rotation transform to bounce rhythmically?

In my design, I have created a simple Spinner icon in Figma. It's meant to be perfectly aligned at the center of an enclosing frame that has dimensions of 64x64. To achieve the rotating effect, I applied CSS rotation as shown below: let rotation = ...