javascript - need to use variable inside a function globally -


i have following javascript code inside function trying soft_left , soft_top variable used in other functions. having little if success. shown relevant code.

function drawcart() { $(".soft_add_wrapper").multidraggable({"containment":"#content"}); $(".soft_add_wrapper").draggable({stop: function(event, ui) {    soft_left = $(".ui-draggable").css('left');    soft_top = $(".ui-draggable").css('top');} } 

when put alert(soft_top) alerts value.

$(".soft_add_wrapper").draggable({stop: function(event, ui) {         soft_left = $(".ui-draggable").css('left');         soft_top = $(".ui-draggable").css('top');        alert(soft_top);} 

when put alert here nothing.

function drawcart(index) { $(".soft_add_wrapper").multidraggable({"containment":"#content"}); $(".soft_add_wrapper").draggable({stop: function(event, ui) {       soft_left = $(".ui-draggable").css('left');       soft_top = $(".ui-draggable").css('top');      }      alert(soft_top); } 

and when try alert(soft_top) in function doesn't work. understand , have read scope , global variable , evil stuff. great!!

consider storing in document via jquery's data() method - global variables annoying maintain.

$(document).data("myvar", 12345); $(document).data("myvar"); // returns 12345 

edited: more concrete example:

// soft_left = $(".ui-draggable").css('left'); $(document).data("soft_left", $(".ui-draggable").css('left'));  // somewhere else $(document).data("soft_left") 

and yes, use hidden text-field - lose type. use

$('#textfield-soft-left').val($(".ui-draggable").css('left')); $('#textfield-soft-left').val(); // returns string 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -