Displaying various image previews in separate divs from user input

Is there a way to allow users to upload multiple images from their computer and have them displayed in separate divs on the webpage? I want users to be able to select images using input fields and then see the uploaded images in different div containers.

HTML

<form id="form" runat="server">
    <input type='file' class="img" />
    <input type='file' class="img" />
    <input type='file' class="img" />
</form>  

<div>
  <img class="image" src="#" alt="image 1" /> 
</div>
<div>
  <img class="image" src="#" alt="image 2" /> 
</div>
<div>
  <img class="image" src="#" alt="image 3" /> 
</div>

JQuery

function readURL(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('.image').attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$(".img").change(function(){
    readURL(this);
});

Answer №1

function displayImage(input) {
    if (input.files && input.files[0]) {
        var reader = new FileReader();
        var div_id  = $(input).attr('set-to');
        reader.onload = function (e) {
            $('#'+div_id).attr('src', e.target.result);
        }

        reader.readAsDataURL(input.files[0]);
    }
}

$(".img").change(function(){
    displayImage(this);
});
img{
  height : 50px;
  width: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form" runat="server">
    <input type='file' class="img" set-to="div1" />
    <input type='file' class="img" set-to="div2" />
    <input type='file' class="img" set-to="div3" />
</form>  

<div>
  <img class="image" id="div1" src="#" alt="image 1" /> 
</div>
<div>
  <img class="image" id="div2" src="#" alt="image 2" /> 
</div>
<div>
  <img class="image" id="div3" src="#" alt="image 3" /> 
</div>

Feel free to utilize this code to enhance your project. I've utilized the Div Id as the set-to attribute for the Input Image Tag and before inserting the image value into the div, I am setting the value of the div accordingly.

Cheers!

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

Angular-nvd3: maintaining consistent spacing between data points along the x-axis

By default, the scale of the x-axis is calculated from values, resulting in uneven distances between two adjacent points. For example, with an array of values like [1,2,5], there will be different distances on the x-axis for each point, and the x-axis labe ...

How can I trigger a click event on a link using JQuery?

One of my links has the unique id: nyhedsklik There is a function associated with this link that activates when it is clicked: $('a.poplight[href^=#]').click(function() { var popID = $(this).attr('rel'); //Fetching Popup ...

What is the best way to initiate animation on a child element once the parent element has reached 75% completion of its animation

Is there a way to use JavaScript to determine when an animation is 75% complete? I have many nested HTML elements that need to be animated with an animation delay property, where the nested animation should start when the parent element is 75% complete. I ...

Creating a fresh HTML document when logging in

Upon successfully logging in, I am unable to access the file success.html. This is my code on the client side <!DOCTYPE html> <html> <head> <title>Login Page</title> <style> //CSS Styles </style> ...

Having trouble with a tslint error in Typescript when creating a reducer

I encountered an error while working with a simple reducer in ngRx, specifically with the on() method. https://i.sstatic.net/9fsvj.png In addition, I came across some errors in the reducer_creator.d.ts file: https://i.sstatic.net/sWvMu.png https://i.ss ...

Determine whether the request was made using ajax or traditional non-ajax methods

While testing the PHP code below, I noticed that the headers in the request do not indicate that it is JavaScript sending the request instead of a non-JavaScript user: Accept:*/* Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 AlexaToolba ...

Error message: "Upon initial loading, Angular and Firebase are unable to map undefined"

After the initial load, it functions properly. However, I am seeking a way to implement a promise to prevent data mapping before it has fully loaded. Upon the first loading of the site, the error below is displayed. This issue may arise from attempting to ...

Next.js - Exceeding Optimal Bundle Size by Approximately 3 Megabytes

Is there a way to decrease the size of my Next.JS bundle, specifically the Node Modules that are causing excessive loading time and slowing down browsing speed? Check out this analysis for more details: https://i.sstatic.net/3LQz4.jpg I'm happy to p ...

Exploring the possibilities of integrating Keycloak with the powerful nuxt-auth

I am incorporating this particular authentication module in conjunction with Keycloak. In my nuxt.config.js configuration: keycloak: { _scheme: 'oauth2', client_id: 'client-bo', userinfo_endpoint: 'SERVER/protocol/open ...

Creating an HTML table by populating it with data from a JavaScript array

Imagine you have a JavaScript array structured like this: [ "Heading 1", "Heading 2", "Data 1", "Data 2", "Data 3", "Data 4", ] Your task is to generate an HTML table from this array that looks like the one in this link: https://i.stack.imgur.com/7laUR. ...

How does CSS affect the size and performance of a website?

Examining the content of this css file (focusing on the last 5 lines specifically): #page section .s_1 { overflow:hidden; width:435px; float:left;} #page section .s_1 h1 { font-size:36pt; color:#001744; line-height:1.1; margin-bottom:64px;height:107px;} # ...

Utilize jQuery to trigger the click action for every Submit button within a Div element

I have a form that contains multiple submit buttons. Before the form is submitted, the following jQuery code is executed: $(":submit").click(function () { $("input[name='social']").val($(this).prevAll(".social").html()); $("input ...

Guide to creating animated sections using only CSS as you scroll the webpage

I am searching for a method to add animation effects (using @keyframes, transform...) to certain elements as the user scrolls down the page. For instance: When the offset is 0: the div will have a height of 100px. When the offset is between 0 and 100: th ...

Secure Socket Layer (SSL) combined with Asynchronous

Specifics include Apache2, Tomcat6, jdk1.6, struts2, Ajax, and jQuery. My inquiry is: A website that functions flawlessly on Http mode (Apache2 HTTP + Tomcat + Struts2) is facing issues when attempting to work in Https (Apache2 HTTPS + Tomcat + Struts2) ...

Tips for inserting data into a PHP modal using MySQL and Ajax

Here is the code snippet I've written: <div class="container"> <div class="row"> <div class="col-md-3"> <div id="accordion" role="tablist" aria-multiselectable="true"> <div class="card"> ...

JavaScript for Image Preloading on iOS Devices

Scenario In my project, I have two images called 'car-light.png' and 'car-dark.png'. When a user interacts with the image named car-light.png, it transforms into car-dark.png. Here is the code snippet that represents this functionalit ...

Steps to displaying the functions linked to a Jquery element

Currently diving into the realm of JQuery, I am on a quest to discover a method to access and view the associated methods of an object once it has been created, reminiscent of python's dir() function. ...

Is there a way to design a right border that is shorter in height than the div itself

I've got a box full of social media content below: 1 | 2 | 3 Is there any way to shorten the vertical pipes "|" so they don't extend beyond the height of the div? ...

Transferring dynamic <select> values to a list demonstrates its worth

Whenever the button is clicked, a new select option is added. On removal, the last member of the array is deleted. The goal now is to transfer the selected value to a list below. This snippet showcases the code in question: $scope.langInput = { cou ...

Importing a JSON model into a three.js environment

I have been attempting to showcase a 3D json model using three.js. Despite being new to three.js, I have exhausted all my options and am unsure of what to do next. Every time I try to load the model, I encounter an error that reads: Uncaught TypeError: C ...