How to load image from javascript using Base64?

Asked

Viewed 841 times

1

Good evening, I’m using this plugin: http://ashleydw.github.io/lightbox/#single-image

Here is JSFIDDLE: https://jsfiddle.net/DTcHh/30015/

Follows the code:

HTML:

<button type="button" class="btn btn-primary">Abrir imagem</button>

JS:

$("button[class='btn btn-primary']").click(function(e) {
var imagem = 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAM ... etc'
event.preventDefault();
  $(this).ekkoLightbox({
  //Como carregar imagem a partir daqui
  });
});

It is possible to load image from javascript ?

  • 1

    Just create the img element and set the src attribute to base text 64.

1 answer

1


With jQuery, you can change the source of the image using attr or prop:

var imagem = 'data:image/jpeg;base64,/9j/...';
$("algum_seletor").attr('src', imagem);

With this it is possible to apply some logic in the ekkoLightbox.

Jsfiddle: https://jsfiddle.net/DTcHh/30020/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.