Reputation: 1078
I have some jquery that is calling a CropBox (to crop an iamge dynamically). All seems to work fine with no errors until after the crop has happened then I get an error:
ReferenceError: recalculateCropBoxHeight is not defined
recalculateCropBoxHeight();
The pertinent code is:
$('#open_admin_crop_window_button').on('click', function() {
myWidth = $('#cropbox').width();
recalculateCropBoxHeight();
function recalculateCropBoxHeight() {
// CHECK IF myWidth HAS RETURNED A PERCENTAGE INSTEAD OF PIXELS
if( myWidth < 100 ) {
myWidth = $(window).width()/(100/myWidth);
}
if(imgw > myWidth) {
widthRatio = imgw/myWidth;
myHeight = imgh/widthRatio;
} else {
myHeight = imgh;
}
xsize = imgw;
ysize = imgh;
}
runCropBox();
});
function cropSuccess(data) {
var returnedData = JSON.parse(data);
if(!returnedData.error) {
jcrop_api.destroy();
recalculateCropBoxHeight(); // ERROR HAPPENING HERE
runCropBox();
}
}
Apologies if my jquery/javascript isn't up to much. I'm very new to this. I will be happy to take suggestions around this but if you could see why this code is giving me the error it would be much appreciated.
As I mentioned, the strange thing is that the code is working fine.??
Upvotes: 0
Views: 262
Reputation: 143
I believe you are running into a scope issue. Because you are defining your function inside of the on click
event, the function cropSuccess(data)
function does not see it as a valid function. Pull the recalculateCropBoxHeight
function out of the event and it should be able to call it.
Upvotes: 1
Reputation: 944442
recalculateCropBoxHeight
is declared inside another function. It is not a global. It does not exist in the scope you are trying to call it from.
Move it outside that function. It appears to only touch variables that are globals anyway.
Upvotes: 1