Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Upload multiple Images Separately and with Different Buttons

I need users to upload several photos, separately and in order, using Html and Javascript. There must be one button per image upload.

I have used JQuery, but it only works with one button, not several.

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

    reader.onload = function(e) {
      $('#image1, #image2')
        .attr('src', e.target.result)
        .width(300)
        .height();
    };

    reader.readAsDataURL(input.files[0]);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container1">
  <img src="..." alt="Profile Image1" id='image1' class="profileImg1">
  <input type='file' class='uploader1' onchange="readURL(this);" />
  <div class="overlay">
    <i class="fas fa-pencil-alt text"></i>
  </div>
</div>


<div class="container2">
  <img src="..." alt="Profile Image2" id='image2' class="profileImg2">
  <input type='file' class='uploader2' onchange="readURL(this);" />
  <div class="overlay">
    <i class="fas fa-pencil-alt text"></i>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You changed both images every time

Instead delegate

Note I gave the ID of the image as a data attribute

I also suggest you use unique IDs instead of unique classnames

const readURL = function() {
  if (this.files && this.files[0]) {
    const reader = new FileReader();
    const preview = this.dataset.preview; 
    reader.onload = function(e) {
      $('#'+preview)
        .attr('src', e.target.result)
        .width(300)
        .height();
    };

    reader.readAsDataURL(this.files[0]);
  }
}
$(function() {
  $("#main").on("change", "[type=file]", readURL)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="main">
  <div class="container1">
    <img src="..." alt="Profile Image1" id='image1' class="profileImg1">
    <input type="file" id="uploader1" data-preview="image1" />
    <div class="overlay">
      <i class="fas fa-pencil-alt text"></i>
    </div>
  </div>


  <div class="container2">
    <img src="..." alt="Profile Image2" id='image2' class="profileImg2">
    <input type="file" id="uploader2" data-preview="image2" />
    <div class="overlay">
      <i class="fas fa-pencil-alt text"></i>
    </div>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!