How to Get the value from function in JQuery

user3654570

Does someone know how can I return the value from a function that contains getJSON The issue is I'm trying to call that function from other function in order to get the value but its all the time undefined!!!! how can I make it work??!?

function TranslateLatLanToAddress(LatLng) {
            var latlng = "34.1574183,-118.437125";
            var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latlng + "&sensor=false";
            $ = jQuery;
            $.getJSON(url, function (data) {
                for (var i = 0; i < data.results.length; i++) {
                    var address = data.results[i].formatted_address;
                    //                 var address = data.results[i].address_components[4].long_name + data.results[i].address_components[6].short_name;
                    //alert(address);
                    //                alert(address);
                    return false;
                }
            });
        }



  function GetVal()
    {
    var retVal =TranslateLatLanToAddress(LatLng);
    alert(retVal); //Its undefined!!!
    }
jasonscript

AJAX calls are asynchronous. This means that while the request is being sent to the server (Google Maps) your script will continue.

This is a good thing because it means your page will continue to load while waiting for the result.

To use this to your advantage, you can use callback methods.

function TranslateLatLanToAddress(LatLng, fnCallback) {
    var latlng = "34.1574183,-118.437125";
    var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latlng + "&sensor=false";
    $ = jQuery;
    $.getJSON(url, function (data) {                                // getJSON is asynchronous
        for (var i = 0; i < data.results.length; i++) {
            var address = data.results[i].formatted_address;        // get the result
            fnCallback(address);                                    // execute the callback from the parameters
        }
    });
}


function GetVal() {
    TranslateLatLanToAddress(LatLng, function(address){             // note the new callback that takes an address as a parameter
        alert(address);                                             // the callback is fired from within the SUCCESS method and now address has a value
    });
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

jQuery plugin how to get value from function within the plugin

From Dev

How to get desired value from autocomplete jquery function?

From Dev

How to get a dynamic JQuery Selector from js function parameter value?

From Dev

How to get desired value from autocomplete jquery function?

From Dev

How to get a value from function?

From Dev

how to get a value from function

From Dev

How to get variable value outside a function IN jquery

From Dev

How to get a value inside a clickHandler function jQuery

From Dev

Jquery: How to get "selectable" Value into "Slider" function?

From Dev

how to get value outside jquery click function

From Dev

How to return value from jQuery function?

From Dev

How to get value from a js function

From Dev

How to get full value from power function

From Dev

How to get posted value from different function?

From Dev

How to get Value from HTML tag in Jquery

From Dev

How to get value from RadioButtonList using jQuery?

From Dev

How to get value from select using jquery

From Dev

How to get the value from GRID using JQuery?

From Dev

How to get XML value from URL with jQuery

From Dev

How to get element value from a form with jQuery

From Dev

How to get XML value from URL with jQuery

From Dev

how to get value from hidden input with jquery?

From Dev

How to get an hidden column value in mRender() function from ajax data source in jQuery datatable

From Dev

How can I get the value of data attribute from a function in WordPress and put it inside a variable in jquery?

From Dev

Get the value from the function

From Dev

Get php array from jquery serialize function sended value

From Dev

Get value from object within JQuery each function

From Dev

How to get value from one function to another function?

From Dev

jquery: How to Get value from array of JSON Object by another value

Related Related

  1. 1

    jQuery plugin how to get value from function within the plugin

  2. 2

    How to get desired value from autocomplete jquery function?

  3. 3

    How to get a dynamic JQuery Selector from js function parameter value?

  4. 4

    How to get desired value from autocomplete jquery function?

  5. 5

    How to get a value from function?

  6. 6

    how to get a value from function

  7. 7

    How to get variable value outside a function IN jquery

  8. 8

    How to get a value inside a clickHandler function jQuery

  9. 9

    Jquery: How to get "selectable" Value into "Slider" function?

  10. 10

    how to get value outside jquery click function

  11. 11

    How to return value from jQuery function?

  12. 12

    How to get value from a js function

  13. 13

    How to get full value from power function

  14. 14

    How to get posted value from different function?

  15. 15

    How to get Value from HTML tag in Jquery

  16. 16

    How to get value from RadioButtonList using jQuery?

  17. 17

    How to get value from select using jquery

  18. 18

    How to get the value from GRID using JQuery?

  19. 19

    How to get XML value from URL with jQuery

  20. 20

    How to get element value from a form with jQuery

  21. 21

    How to get XML value from URL with jQuery

  22. 22

    how to get value from hidden input with jquery?

  23. 23

    How to get an hidden column value in mRender() function from ajax data source in jQuery datatable

  24. 24

    How can I get the value of data attribute from a function in WordPress and put it inside a variable in jquery?

  25. 25

    Get the value from the function

  26. 26

    Get php array from jquery serialize function sended value

  27. 27

    Get value from object within JQuery each function

  28. 28

    How to get value from one function to another function?

  29. 29

    jquery: How to Get value from array of JSON Object by another value

HotTag

Archive