Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery save local variable for use later on in the code

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.

like image 633
Sol Avatar asked May 30 '11 19:05

Sol


People also ask

How can we store data in variable in jQuery?

jQuery(element). data(key,value); // Store arbitrary data associated with the matched elements. or declare your variable outside the function.

What does $() mean in jQuery?

In jQuery, the $ sign is just an alias to jQuery() , then an alias for a function. This page reports: Basic syntax is: $(selector).action() A dollar sign to define jQuery. A (selector) to "query (or find)" HTML elements.

How do I save a variable in Javascript?

You can read about Local Storage here. // save data value localStorage. setItem("name", "John"); // retrieve data value var name = localStorage. getItem("name");


1 Answers

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);
like image 79
Thomas Avatar answered Nov 03 '22 07:11

Thomas