Solved

Javascript variable scope

Posted on 2011-03-23
8
302 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:Huseyin KAHRAMAN
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:Huseyin KAHRAMAN
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:sahanz
ID: 35204140
@HainKurt:

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

Expert Comment

by:Huseyin KAHRAMAN
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Video Tutorial help 2 37
Popup write two lines 3 22
Check input text, Number 6 27
sort Multi-dimensional array 6 8
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

740 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