0

Can someone help me with this code please? I can't get it to save photos to localStorage. I am new at this and I'm trying to save multiple images to localStorage.

<div class="images-grid">
<div class="upload">
    <div class="round" id="display-image">
    <input type="file" id="image-input" accept="image/jpeg, image/png, image/jpg">
    <i class = "fa fa-camera" style = "color: #000;"></i>
    </div>
  </div>
  <div class="upload">
    <div class="round" id="display-image2">
    <input type="file" id="image-input2" accept="image/jpeg, image/png, image/jpg">
    <i class = "fa fa-camera" style = "color: #000;"></i>
    </div>
  </div>
</div>

javascript:

const image_input = document.querySelector("#image-input");

image_input.addEventListener("change", function() {
  const reader = new FileReader();
  reader.addEventListener("load", () => {
    const uploaded_image = reader.result;
    document.querySelector("#display-image").style.backgroundImage = `url(${uploaded_image})`;
  });
  reader.readAsDataURL(this.files[0]);
});

const image_input2 = document.querySelector("#image-input2");

image_input2.addEventListener("change", function() {
  const reader = new FileReader();
  reader.addEventListener("load", () => {
    const uploaded_image = reader.result;
    document.querySelector("#display-image2").style.backgroundImage = `url(${uploaded_image})`;
  });
  reader.readAsDataURL(this.files[0]);
});
Evelina
  • 1
  • 1
  • Where's the JS? – Johna Oct 30 '22 at 16:47
  • 1
    Check this question as well: https://stackoverflow.com/questions/19183180/how-to-save-an-image-to-localstorage-and-display-it-on-the-next-page – Sigma Oct 30 '22 at 17:37
  • Also see: [What happens when localStorage is full?](https://stackoverflow.com/questions/13567509/what-happens-when-localstorage-is-full) – Yogi Oct 30 '22 at 17:47

0 Answers0