jQuery : Passing Variable Value From On Click Event Failed

I have this javascript :

<?
var dropzone_maxfiles = '.$product_maxfiles.';

$( ".product_imgfiles" ).click(function(event) {
    event.preventDefault();
    var img_file = $( this ).children( "img" ).attr( "src" );

    $.ajax({
        url: "controller/ctrl.dropzonejs-cleaner.php?token='.$product_token.'&key='.$secret_token.'&imgfile="+img_file+""
    });

    $( this ).parent( "div" ).remove();                 
    dropzone_maxfiles = dropzone_maxfiles + 1;
});

var myDropzone = new Dropzone(".dropzone", { 
    url: "controller/ctrl.dropzonejs.php?token='.$product_token.'&key='.$secret_token.'",
    maxFilesize: 2,
    maxFiles: dropzone_maxfiles,
    acceptedFiles: ".jpeg, .jpg, .png, .gif"
});

myDropzone.on("complete", function() {
    var rejected = myDropzone.getRejectedFiles();

    if (rejected == "" ) {
        $(".btn-vendor-addnew").removeClass("disabled");
        $(".dropzone-error").css("display","none");
    }else {
        myDropzone.removeAllFiles(true);
        $(".btn-vendor-addnew").addClass("disabled");
        $(".dropzone-error").css("display","block");

        $.ajax({
            url: "controller/ctrl.dropzonejs-cleaner.php?token='.$product_token.'&key='.$secret_token.'"
        });
    }
});
?>

the problem is : dropzone_maxfiles = dropzone_maxfiles + 1; value doesn't exist in maxFiles: dropzone_maxfiles,

how to pass that dropzone_maxfiles variable from on click event into Dropzone maxFiles? thank you.

Answers 1

  • Like @adeneo said in the comments, the property maxFiles of the Dropzone object will not change automatically if the variable is changed.

    Instead, try reinitializing it with the current variable on click, or perhaps just change that property value (I'm not sure exactly how Dropzone is implemented, so there might be some setter method to do this for you).


    EDIT (upon request): a basic example:

    $( ".product_imgfiles" ).click(function(event) {
        // ... event handling code          
        dropzone_maxfiles = dropzone_maxfiles + 1;
    
        // add this to "reset" the Dropzone
        myDropzone = new Dropzone(".dropzone", { 
            url: "controller/ctrl.dropzonejs.php?token='.$product_token.'&key='.$secret_token.'",
            maxFilesize: 2,
            maxFiles: dropzone_maxfiles,
            acceptedFiles: ".jpeg, .jpg, .png, .gif"
        });
    
    });
    

Related Articles