May 2, 2012

jQuery change var by event in real time – not work

Question by Михаил Дмитриев

How make change “aspectRatio” in settings https://github.com/trentrichardson/UberUploadCropper/blob/master/example-advanced/index.php in real time.

$(function() {

$.("#test1").click( function () {
    $ratio = 1;
});
$.("#test2").click( function () {
    $ratio = 2;
});

$('#UploadImages').uberuploadcropper({

...

'aspectRatio': $ratio, 

It not work. Why?
How it is correct to make?

Answer by Starx

Define a global variable of the $ratio.

Example:

var $ratio; //Define a global variable like this
$(function() {

    $("#test1").click( function () {
        $ratio = 1;
        initPlugin();
    });
    $("#test2").click( function () {
        $ratio = 2;
        initPlugin();
    });

    function initPlugin() {    
        $('#UploadImages').uberuploadcropper({

             ...

             'aspectRatio': $ratio, //now the value will be taken from global scope
        });
    }
});

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!