15

Is there anyway that I can save or access a local variable outside of it's function? Consider the code below:

$( "#droppable2" ).droppable({
        activeClass: "ui-state-hover",
        hoverClass: "ui-state-active",
        accept: "#draggable3",
        drop: function( event, ui ) {

            jdc = $(this).attr("id"); //I need to use this value later
            $( this )
                .addClass( "ui-state-highlight" );
                var x = ui.helper.clone();   
                x.appendTo('body');

                var jdi = $("img").attr("id");// I need to use this value later

                $(this).droppable( 'disable' );
        }
    });

Is there anyway to get the values of the two variables (jdc and jdi above) for later use outside of the function?

The ultimate goal is to get id of droppable container and content of dropped element.

4
  • you can define them as global variable right Commented May 30, 2011 at 19:33
  • I ain't 100% sure but just try not declaring with var at initialisation should make it global. Commented May 30, 2011 at 19:36
  • @kobe, thanks for the response. If i define them as global variable, am I able to change their values locally within the function and get the updated value outside of the function? Sorry if I'm sounding naive, I'm new to this. Also the project will have about three of such functions with local variables whose values i would need to call later on in the script. But i figured that if I get this one function to work, I can proceed with the others. Thanks Commented May 30, 2011 at 19:41
  • you can , but as others suggested you can use .data Commented May 30, 2011 at 20:06

3 Answers 3

23

try this:

jQuery(element).data(key,value);
// Store arbitrary data associated with the matched elements.

or declare your variable outside the function.

var example;

function abc(){
   example = "12345";
}

abc();

console.log(example);
Sign up to request clarification or add additional context in comments.

1 Comment

Just to be explicit (for any newbies out there - I am one and this is what I was doing wrong), declaring the variable (var example;) outside the function and defining the variable (example = "12345";) inside the function is the key to this answer. It won't work if you declare + define the variable (var example = "12345";) inside the function - this will only apply locally (inside the function). In short, to save a local variable globally, declare (use var) outside the function and define (don't use var) inside the function.
1
var jdc;
var jdi;    

$( "#droppable2" ).droppable({
    activeClass: "ui-state-hover",
    hoverClass: "ui-state-active",
    accept: "#draggable3",
    drop: function( event, ui ) {

        jdc = $(this).attr("id"); //I need to use this value later
        $( this )
            .addClass( "ui-state-highlight" );
            var x = ui.helper.clone();   
            x.appendTo('body');

            jdi = $("img").attr("id");// I need to use this value later

            $(this).droppable( 'disable' );
    }
});

1 Comment

Thanks for the initiative Thomas; and Kei for the implementation. MASSIVE THANKS to everyone. Works like a charm. :D
0

You could always just store them as data on the element:

$(this).data('jdi', $('img').attr('id'));

Then you can get it back from that element with another call to ".data()":

var theSavedJdi = $('whatever').data('jdi');

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.