0

How to use the variable "i" and the value in another function, "izq" and "der"?. I need to use the variable within other functions.

    $("#gal li a").on('click', openImg);

    function openImg(e){
        e.preventDefault();
        var href = $(this).attr('href');
        i = $(this).parent().index();
        $("#overlay").fadeIn();
        $("#overlay #cont_img img").attr('src', href);
    }

    $(".izq").on('click', function(e){
        e.preventDefault();
        var izq;
        i--;
        if(i < 0){
            i = total-1;
        }
        izq = $("#galeria li a").eq(i).attr('href');
        $("#overlay #cont_img img").attr('src', izq);
    });

    $(".der").on('click', function(e){
        e.preventDefault();
        var der;
        i = (i-1) % total;
        der = $("#gal li a").eq(i).attr('href');
        $("#overlay #cont_img img").attr('src', der);
    });
4
  • Declare those variables within scope of all the functions you need to use them in Commented Apr 25, 2016 at 7:34
  • But then if i is modified by a function the new functions will not have the new value of i Commented Apr 25, 2016 at 7:37
  • It will have updated value... Do share an example where you face such issue... Commented Apr 25, 2016 at 7:38
  • I need to update the value and share in the other functions Commented Apr 25, 2016 at 8:41

5 Answers 5

1

Make the variable global - put it in the global scope - http://www.w3schools.com/js/js_scope.asp

Sign up to request clarification or add additional context in comments.

Comments

0

It is best if you can declare your variable outside the function as globally.

$("#gal li a").on('click', openImg);

var i=0;

    function openImg(e){
        e.preventDefault();
        var href = $(this).attr('href');
        i = $(this).parent().index();
        $("#overlay").fadeIn();
        $("#overlay #cont_img img").attr('src', href);
    }

    $(".izq").on('click', function(e){
        e.preventDefault();
        var izq;
        i--;
        if(i < 0){
            i = total-1;
        }
        izq = $("#galeria li a").eq(i).attr('href');
        $("#overlay #cont_img img").attr('src', izq);
    });

    $(".der").on('click', function(e){
        e.preventDefault();
        var der;
        i = (i-1) % total;
        der = $("#gal li a").eq(i).attr('href');
        $("#overlay #cont_img img").attr('src', der);
    });

Comments

0

If you put var i outside those functions you should be able to use it in all functions

Comments

0

Though you can make i a variable which is in global scope. But this may result in conflict. An better way is to use the namespace pattern . For Example

Create an object

var myGlobalVariables ={};

Now you can add a key to this object.

function openImg(e){
        e.preventDefault();
        var href = $(this).attr('href');
        myGlobalVariables.i = $(this).parent().index();
        $("#overlay").fadeIn();
        $("#overlay #cont_img img").attr('src', href);
    }

And in all other functions you can use the same object & key. In other functions you can access it using var m = myGlobalVariables.i;

Comments

0

You can make variables global, use them a part of an object in higher scope etc. or you can use a closure

// prints 0..9
for(var i = 0; i < 10; i++) { console.log(i); };

// any amount of other operations

// prints 10 three times
for(var z = 0; z < 10; z++) { console.log(z); };

you can read more about these things here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.