Solved

Javascript variable scope

Posted on 2011-03-23
8
300 Views
Last Modified: 2012-05-11
Hello, I have this function.
function save_to_db(item)
    {
        var widget_id;
        widget_name = item.attr('id'); //ie - image_widget
        alias = widget_name.replace("_widget", "");

        $.ajax({
         //ajaxsetup ftw
          data: {
              'action': 'add_element',
              'page_id': '106034386126175',
              'element_id' : alias
          },
          success: function(msg)
                   {
                       widget_id = msg;
                   }
        });

        return widget_id;
}

Open in new window


I want this function to return the data which comes from the ajax success, however it's always returning "undefined" and the ajax success works correctly. Any idea?
0
Comment
Question by:sahanz
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 51

Expert Comment

by:HainKurt
ID: 35204131
try

success: function(msg)
                   {
                       widget_id = msg; alert(widget_id);
                   }

what does it show?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 35204135
it cannot come from success handler, since success handler is a call back handler which is called asynchronously.

what you need to do is, that the method which needs the widget id needs to get called from the success handler.
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35204137
I guess you should use async=false

async: false
$.ajax({
         //ajaxsetup ftw
          async: false,
          data: {
              'action': 'add_element',
              'page_id': '106034386126175',
              'element_id' : alias
          },
          success: function(msg)
                   {
                       widget_id = msg;
                   }
        });

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 1

Author Comment

by:sahanz
ID: 35204140
@HainKurt:

Works fine, widget_id alerts the correct value.
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 35204175
I guess you should change your logic a bit :) if you do not want to use async
when it succeded, call another function to do the remaining jobs (if any)

success: function(msg)
                   {
                       widget_id = msg; fnDataSaved(msg);
                   }

but did you try async? it may be what you want...
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35204336
we need to add : .responseText;

var widget_id = $.ajax({ url: "some.php", async: false }).responseText;

so :


function save_to_db(item) {
   widget_name = item.attr('id'); //ie - image_widget
   alias = widget_name.replace("_widget", "")
   var d = data: { 'action': 'add_element', 'page_id': '106034386126175', 'element_id' : alias }
   return $.ajax({ url: "some.php", async: false }).responseText;
}

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 35204338
oups I forget to add the data line 5 :


return $.ajax({ url: "some.php", data:d, async: false }).responseText;

Open in new window

0
 
LVL 1

Author Closing Comment

by:sahanz
ID: 35205350
Cool, that's what I wanted :)
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question