jessica
jessica

Reputation: 495

Set image data into image src

I have a function that gets the image data on click, the image data is defined as a variable, what i want to do is to take this image data and set it into an image src, how can i do that? here is my code:

    $('.export').click(function() {
      var imageData = $('.image-editor').cropit('export');
  
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<img src=''>

Upvotes: 0

Views: 2668

Answers (2)

Try jquery .attr()

Update your image element <img id='profile-pic' src=''>

$('.export').click(function() {
    var imageData = $('.image-editor').cropit('export');
    $('#profile-pic').attr('src', imageData); //Change your img src to imageData
});

Update 1: For base64

$('#profile-pic').attr('src', 'data:image/png;base64,' + imageData);

Upvotes: 1

Bubble Hacker
Bubble Hacker

Reputation: 6723

You can use the jQuery attr() function:

$('img').attr('src', 'data:image/png;base64,' + imageData)

See more about the function - http://api.jquery.com/attr/

Upvotes: 2

Related Questions