Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jcrop changing image src after page load

I am using jcrop for image cropping on the fly and want the user to have the option of changing the image before they start to crop.

For some reason I cannot get the image to change.

jsfiddle http://jsfiddle.net/UUKP4/2/

When I look at the code in firefly it shows two img elements and one being inside a jcrop container with no id.

how can I change the image?

code

#jcrop_target {
width:200px;
}


$(function () {
$('#jcrop_target').Jcrop();
});

function chageImage() {
alert("hello");
$('#jcrop_target').attr('src', 'http://eofdreams.com/data_images/dreams/cat/cat-06.jpg');
}

<img src="http://jcrop-cdn.tapmodo.com/v0.9.10/demos/demo_files/pool.jpg" id="jcrop_target" />
<input type='button' value='change image' id='changeImage' onclick='chageImage();'>
like image 282
Barry Watts Avatar asked Aug 21 '13 07:08

Barry Watts


2 Answers

JCrop creates a copy of your image to use for cropping. You just need to change the selector like so:

$('.jcrop-holder img').attr('src', 'http://eofdreams.com/data_images/dreams/cat/cat-06.jpg');

http://jsfiddle.net/UUKP4/4/

like image 103
Dragony Avatar answered Nov 14 '22 23:11

Dragony


You can use the setImage() function. When you initialise Jcrop, save the object it creats, so you can call this function when required.

var JCropper;

$('#image').Jcrop({ 
  ..setup..
} function() {
  // Save Jcrop object
  JCropper = this;
}

JCropper.setImage('/newimage.jpg');
like image 20
JDandChips Avatar answered Nov 14 '22 22:11

JDandChips