-3

I have this image element with an id. I am trying to store it as a variable so that I can use it. I have my code but when I run it, instead of a display image, it shows me [object Object]. Please, how do I store images in a variable and gain access to it?

html:

<img width="100" height="100" id="image"/>

Javascript:

function myTest(){
  var image =  $("#image").attr("src", "data:image/jpeg;base64,");
}
000
  • 26,951
  • 10
  • 71
  • 101
Godymn
  • 23
  • 7

1 Answers1

0

If for some reason you want the actual base64 data of the image to use later, and not just the src, then you'd do something like

fetch(srcOfImg)
  .then(res => res.blob())
  .then(blob => {
    const reader = new FileReader();
    reader.onloadend = () => {
      const newImg = document.body.appendChild(document.createElement('img'));
      newImg.src = reader.result;
    }
    reader.readAsDataURL(blob);
  });
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320