Solved

Javascript variable scope

Posted on 2011-03-23
8
294 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
 
LVL 1

Author Comment

by:sahanz
ID: 35204140
@HainKurt:

Works fine, widget_id alerts the correct value.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now