0

in my user.data.crop_position value is "[ 100, 100, 200, 200 ]";

            var crop_position=user.data.crop_position.slice(1,user.data.crop_position.length-2);
        $('#cropbox').Jcrop({
            setSelect: crop_position,
            onChange: showPreview,
            onSelect: showPreview,
            aspectRatio: 1
        });

doing this my jcrop is not select at set postion what can i do it is due to the string i am passing in how can i remove this ,

i know this is silly question but i got these kind of problem many time also please suggest me that in future these kind of problem dont came.

regards

rahul

Pointy
  • 405,095
  • 59
  • 585
  • 614
XMen
  • 29,384
  • 41
  • 99
  • 151
  • what's the value of user.data.crop_position when user.data.crop_position != '' in the above and why are you slicing it? Haven't you stored the array as you need it? Why slice? – Steve Claridge Nov 13 '10 at 11:53
  • user.data.crop_position value is "[ 100, 100, 200, 200 ]" – XMen Nov 13 '10 at 11:56
  • i am doing slicing for removing " double quotes a i am thinking due to double quotes it is cretaing problem – XMen Nov 13 '10 at 11:57
  • where are you setting the `crop_position` value, is it something you can change? – Nick Craver Nov 13 '10 at 12:04

2 Answers2

0

setSelect - array [ x, y, x2, y2 ] Set an initial selection area

So you need an array not a string for setSelect. Why don't you make user.data.crop_position an array itself? If there is no way to change the representation you can do the conversion with a simple algorithm:

var pos  = '"[ 100, 100, 200, 200 ]"'; // user.data.crop_position
var crop_position = pos.replace(/["\[\] ]/g, '').split(',');
for (var i = crop_position.length; i--;) {
  crop_position[i] = +crop_position[i]; 
}

Now you've got an array of values instead of a string.

gblazex
  • 49,155
  • 12
  • 98
  • 91
0

The Jcrop Manual says that setSelect takes an array, not a string.

[100, 100, 200, 200] // rather than
'[100, 100, 200, 200]'

If you can't change the input format, at least you can parse it using $.parseJSON before passing it to Jcrop:

var crop_position = $.parseJSON(user.data.crop_position);

Edit: If necessary (double quotes are actually present in the string value), you can use $.parseJSON twice, first to decode the encoded string value and second to decode the array within the encoded string:

    var crop_position = $.parseJSON($.parseJSON(user.data.crop_position));

Or just strip off the surrounding double quotes before $.parseJSON:

    var crop_position = $.parseJSON(user.data.crop_position.slice(1, -1));    
PleaseStand
  • 31,641
  • 6
  • 68
  • 95