Creating a jQuery multiple image preview feature is a simple and effective way to

Looking to implement a single image upload preview function in jQuery? Want to modify it to allow for multiple image uploads with separate boxes? Check out the HTML script below.

<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<script>

function imagepreview(input){
    if(input.files && input.files[0]){
        var filerd = new FileReader();
        filerd.onload=function(e){
            $('#imgpreview').attr('src', e.target.result);
        };
        filerd.readAsDataURL(input.files[0]);
    }
  }

</script>
<style>
body{
font-family:arial;
}
.wrap{
border:0px solid;
width:40%;
margin:10px auto;
}

table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
    text-align:center;
    padding:10px;
}
#idupload{
    display:none;
}
.input-label{
    background-color:#009688;
    color:#fff;
    padding:5px 15px;
}
</style>
</head>
<body>

<div class="wrap">
<table>
  <tr>
    <th><img id="imgpreview" src="" width="100" height="100"></th>
    <th><img id="imgpreview" src="" width="100" height="100"></th> 
    <th><img id="imgpreview" src="" width="100" height="100"></th>
  </tr>
  <tr>
    <td><label for="idupload" class="input-label"><i class="fa fa-arrow-up"></i>upload</label><input type="file" id="idupload" onchange="imagepreview(this);" /></td>
    <td><label for="idupload" class="input-label"><i class="fa fa-arrow-up"></i>upload</label><input type="file" id="idupload" onchange="imagepreview(this);" /></td>
    <td><label for="idupload" class="input-label"><i class="fa fa-arrow-up"></i>upload</label><input type="file" id="idupload" onchange="imagepreview(this);" /></td>
  </tr>
</table>
</div>

</body>
</html>

Answer №1

The issue at hand arises from having duplicate ID's in your codebase. The `imagepreview()` function is consistently updating the same ID with the uploaded image, causing conflicts. To resolve this issue, assign a unique ID to each image preview and synchronize it with the corresponding file upload box. This way, each file upload box will update its own specific image preview.

Take a look at this example I created. By assigning distinct IDs to both the previews and uploads and modifying the `imagepreview()` function to incorporate these IDs, you can ensure that each upload box corresponds to a unique image preview on the page. http://codepen.io/anon/pen/apOyZB

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

Can the Vue instance be accessed in Axios interceptors?

I've integrated vue-reactive-storage to have a reactive alternative to localStorage in my Vue application. This plugin creates a vue object that is accessible to all components. I'm facing an issue when trying to access this object within my axio ...

Responding to a tweet with the help of twit and Node.js

Utilizing the Twit Node.js API has presented me with a challenge. I am attempting to reply to a tweet that contains a specific keyword. My goal is for my response tweet to appear nested underneath the original tweet, much like how replies function on socia ...

Transmit a continuous flow of integer values from an Android application and capture them on a Node.js express server

I'm looking to develop a simple solution to send a continuous stream of integers from an Android App to a Node.js server. I'm interested in understanding how to establish this stream in Android and how to receive it on my Node.js server using ex ...

Issue encountered when employing the spread operator on objects containing optional properties

To transform initial data into functional data, each with its own type, I need to address the optional names in the initial data. When converting to working data, I assign a default value of '__unknown__' for empty names. Check out this code sni ...

Nested JavaScript function expression

In this JavaScript example, the snippet provided is for educational purposes and does not include validation. After defining the variable 'isBetween' within the buildBoundDetector() function, it raises questions about how passing a number through ...

obtain a link that is clickable from an array of options

$('button').on('click', function(){ let selection = document.getSelection().toString().trim(); document.execCommand('createLink', true, selection); }) <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery ...

Using JavaScript to open links in a new tab with the target

document.getElementById("mahacareer").onclick = function () { window.open("http://www.mahacareermitra.in", '_blank'); }; <a href="" id="mahacareer">Access the portal</a> Hi there, I am looking to have the link above open in a new tab ...

Steps for passing a MVC Model in an Ajax Request

I am currently encountering some difficulties when attempting to post a Model to trigger an action in a controller. Within my main view, I initiate a web service call with specific criteria and receive the results. Once I have the results, I display them ...

Generate a see-through overlay when hovering over an image that matches the image's precise dimensions

I have encountered a challenge that I need help with. On a webpage, there are multiple images of varying sizes. I am looking to create a feature where hovering over an image triggers the appearance of a div above it containing a button and text without dis ...

Can parameters be included in the HTML class attribute?

Is it possible to embed specific information into HTML elements, like images, without disrupting valid HTML markup? Would the following example be considered valid HTML? <img src="..." class="isearcher:tags(paris+hilton,gary+suneese)" > The idea is ...

Creating a changing text color using Material UI and React

How can one dynamically set the text color based on the background color of a component using Material-UI API? If component A has a light background, then the text should be dark. For component B with a black background, the text should be light. (I have ...

Showcasing a JavaScript array retrieved through JSON on a web page

Apologies if I am causing any inconvenience again =x Thanks to the assistance from everyone, especially Scott Harwell, I successfully passed my php array to a js array using the code below: var horse_array = <?php echo json_encode($horse_info);?>; ...

How to vertically align Bootstrap5 buttons on mobile devices

I'm currently developing a web application and I'm facing an issue with centering two bootstrap buttons next to each other on mobile devices. While it works fine on the computer, when I inspect the page on a mobile device, the buttons stack verti ...

Modify HTML files prior to transmission from an express server

This method comes in handy when you only have a few elements that need to be updated on your webpage, offering a seamless way to refresh the content without the need for additional HTTP requests or frontend manipulation. ...

How can one identify a concealed glitch that exclusively occurs for a particular individual or hardware in a React environment?

Is it possible to identify a bug that occurs only with a particular individual or hardware in a React application? This bug is invisible and never appears during tests, but only manifests with a specific client within my company. Do you have any tips on h ...

Navigate through pages using scrollspy without losing your position when returning

Hey all you web geeks out there, I could really use your help solving a little issue. I've been working with Bootstrap to build a website that utilizes scrollspy for navigating different sections of the page using the navbar. The only way I could sto ...

The CSS :lang() selector targets elements within documents that do not specify a particular language

Can elements that do not have a language set or inherited, meaning they are in an unspecified ("unknown") language, be targeted? Facts The language of an HTML document or element can be specified using the HTML lang attribute, for example: <html lang=& ...

"Obtaining a MEAN response after performing an HTTP GET

I'm currently in the process of setting up a MEAN app, however I've hit a roadblock when it comes to extracting data from a webservice into my application. While returning a basic variable poses no issue, I am unsure how to retrieve the result fr ...

Challenges in the Knockout Framework Due to Synchronization Issues

Recently, I've encountered a slight problem with race conditions in my code. Utilizing Knockout.Js to collect information for user display has been the cause. The issue arises when a dropdown needs to be created before a value can be selected. Typica ...

Tips for setting up a range slider for decimal numbers

I have implemented the following code for a range slider. It works perfectly fine for integer values like 1 and 100, but I am facing issues when trying to use decimal values. I attempted to substitute 1 with 0.1 but it did not yield the desired result. ...