Ways to dynamically add a div and an image together

Is there a way in JavaScript to append a div and an image inside it?

Basically, I have images stored in an array. The displayAllImages function appends the images to the #images div. However, I want this function to append a new div, and within that div, there should be an image, either as a background or contained within a div. This process should repeat for all images so that each image is displayed in its own div. Please use only array implementation. Thank you!

Jsfiddle : http://jsfiddle.net/1qk3voxq/


            var gallery= new Array();

            gallery[0]="http://s6.tinypic.com/1zd1a47_th.jpg";
            gallery[1]="http://s6.tinypic.com/2ngh9ty_th.jpg";
            gallery[2]="http://s6.tinypic.com/29zy5qf_th.jpg";

            var x= $("#images");

            function displayAllImages() {
                var i = 0,
                    len = gallery.length;
                for (; i < gallery.length; i++) {

                   x.append("<img class='roll' src='" + gallery[i] + "'>");
                }
            };

            $(function() {
                displayAllImages();
            });
        

            #container {
                width:600px;
                height:600px;
                background-color:#000;
            }

            .roll {
                margin:4px;
            }
        
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="container">
    <div id=images></div>
</div>

Answer №1

Do you mean something like this?

x.append("<div class='roll'><img  src='" + gallery[i] + "'></div>");

Fiddle Updated

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

Make sure to wait for the complete loading of all content in the AJAX response before showing it on

I am currently working on a search function that sends a json request to the server for results every time a character is entered. Although this part is functioning correctly, I am trying to figure out how to add a loading class to .search-load > .conta ...

Puzzling blank area found beneath the form component

There seems to be some extra space at the bottom of a form when it's placed inside a div. How can we remove that unwanted space? In examples from stack overflow, the extra space is not visible in the code snippets provided. However, it appears elsewh ...

Encountering an unexpected token error when trying to implement jQuery file upload in Django

Check out the jQuery file upload plugin. While working with this plugin, I encountered a conflict between a JavaScript template language and Django's template language. To resolve this, I implemented a custom template tag called "raw" to display the ...

The scraping tool from Snipcart encountered issues while trying to capture product data in a Next.js

I am encountering an issue with Snipcart while using it in a Next.js app integrated with Strapi. The error message I receive when processing a payment is as follows: A 'cart-confirmation' error occurred in Snipcart. Reason: 'product-craw ...

The Vue template syntax utilizes double curly braces for incrementing values within must

Embarked on learning Vue.js recently and I am intrigued by the unusual behavior in my simple code. I cannot seem to figure out what is going wrong. I am attempting to increment a counter inside mustache syntax, but something strange is happening with this ...

Guide on presenting database images using a while loop

I am a relatively new PHP/MYSQL Coder and I am currently working on creating a category browse page. On this page, I need to display 50 images from a mysql database named "inventory". I have managed to generate the links using a while loop, but I am unsure ...

What is the process for incorporating extra classes that receive styling from a distinct CSS file?

Currently, I am utilizing both react-bootstrap and JSS concurrently, which may not be the most optimal approach. However, it is what I have been working with. I am attempting to apply a combination of Bootstrap classes while also incorporating styling thro ...

Enhance Your Webpage with jQuery Drag and Drop Feature Across Multiple Lists

I've been successfully using "jQuery UI Sortable connectwith" for drag & drop functionality, as shown in the example here: . However, I now need to implement drag & drop with multiple UI elements and it's not functioning correctly. Example: < ...

Tips for refining search criteria with a combination of checkbox and range slider in Angular 2

In an attempt to refine the results for the array "db," I have implemented three filters: price, duration, and category. I have experimented with using the filter() method to apply these filters. You can find the code I have worked on here: https://stack ...

Reorganizing a Multidimensional Object in JavaScript After Removing a Nested Object

I am facing an issue with deleting a nested object based on index and i in Vuejs methods. The current code is not working as expected, as it deletes multiple objects instead of just the one specified. Additionally, sometimes the indexing is incorrect but I ...

Uploading Files Using JSON with Javascript and Jquery

I need to create an HTML list with a single li element named import. Behind it, there should be an input type ="file" that is initially hidden. When the user clicks on import, it should trigger the file upload from the hidden input field using .click(). ...

Switch tabs with JavaScript

I'm encountering an issue with changing tabs using JavaScript and Ajax. The script I have works when not using Ajax but fails to work properly with it. Here is the script: $(document).ready(function(){ $("#mtabs li").click(function() { ...

Steps for enabling file downloads on an HTML server

I've configured an HTML Server on Windows Server 2012 with several virtual directories and files added. When attempting to download a file named firmware.dob, I'm encountering an issue where the browser displays "File not found." 404 - File or ...

Is it possible to input tab characters in a TextField?

I am working with a multi-line MUI TextField and my objective is to input JSON text. However, I encountered an issue where pressing the tab key causes the component to lose focus and shift to the next element on the page. Is there a way to prevent the ta ...

Can one access console and localstorage through android studio?

Is there a way to detect if LocalStorage is being saved on an Android device from Android Studio in an application with a WebView? Also, can javascript code be executed from Android Studio similar to running it in a Chrome console? ...

Implementing a Model-View-Controller architecture with AJAX functionality

Recently, I encountered an issue while trying to refresh a div containing a PartialView using Ajax.BeginForm. This process had worked smoothly for me numerous times in MVC4, but with the switch to MVC5, things took a turn. Let me walk you through the step ...

Selection of multiple listings

Looking for a solution to create a multi list selection area? You can double click on an item in the left list to add it to the selected area. Also, you can double click on an item in the selected area to remove it from the list. <ul id="selection"> ...

Switching the order of elements in a column using Flexbox

Here is the grid layout on PC: https://i.sstatic.net/4HRvd.png And here is the grid layout on mobile devices: https://i.sstatic.net/URBjb.png I am looking to rearrange the order and layout of the grid. I have tried using flexbox, but I need to group B an ...

Turn off padding in material ui card

Currently, I am utilizing a material UI card and upon inspecting the card, I noticed the presence of this unique class: "MulticardContent-root". This class adds padding of 16 which I would like to remove. Strangely, it is not located within the styles co ...

What is the measurement of the width of a specific DOM element?

Is it possible to determine the width of a DOM element? Specifically, I need to find out the width of a table cell (td) that may have varying widths based on the length of text inside it. Ideally, I prefer a solution that utilizes the jQuery library, but ...