SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist Sikwondo's Avatar
    Join Date
    Sep 2005
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sitepoint Book: jQuery Nov to Ninja 2 - jCrop code not working?

    Hi all,

    I am working through the 2nd edition of the jQuery Novice to Ninja book and the jCrop code doesn't seem to work, specifically the second part where we are trying to alert the coordinates.

    The specific part is chapter 4 - 03 - jcrop.

    With the second part of the code, all I get is a small black square box, the image is collapsed? And can't select and perform the crop to receive the coordinates.

    I have downloaded the code archive from the Sitepoint website and am seeing the same thing, no image.

    The code I am using is as follows:

    HTML Code:
    <div id="imageCrop">
    	<img src="img/lightbox/deliverus2large.jpg" alt="" id="crop" />
    	<input type="button" value="Perform the crop!" />
    </div><!--/ crop-->
    Code:
    var jcrop = $.Jcrop('#crop',{
    	setSelect: [10,10,300,350],
    	minSize: [50,50],
    	onChange: function(coords) {
    		// use the coordinates
    	},
    	onSelect: function(coords) { // Will only fire when the selection has been defined - when the user has stopped dragging
    		// use the coordinates
    	}
    });
    
    $('#imageCrop :button').click(function(){
            var selection = jcrop.tellSelect(); // tellSelect obtains the current selection - has same properties as the event coordinates
    	alert('selected size: ' + selection.w + 'x' + selection.h); // We aren't sending to the server - so we will just alert instead
    })
    Is there something I am doing wrong?

    Many thanks
    Simon

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    The wrong jQuery wrapper is being used.

    According to the Jcrop API documentation, the way that the book accesses the Jcrop API must be from the window load event, otherwise unexpected behaviour may (and does) occur:

    Code javascript:
    $(window).load(function() {
        var jcrop = $.Jcrop('#mofat', {
            ...
        });
        ...
    });

    With more recent versions of Jcrop, from v0.9.9 onwards, the advised way is to use the ready callback instead and set the variable from within the callback function:

    Code javascript:
    $(document).ready(function () {
        var jcrop;
        $('#mofat').Jcrop({
            ...
        }, function() {
            jcrop = this;
        });
        ...
    });
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •