Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:17:18+00:00 2026-06-07T09:17:18+00:00

My javascript function getLatLng works great when called by the window.onload. But when I

  • 0

My javascript function getLatLng works great when called by the window.onload. But when I use the handler to call it I get a position (NaN, NaN) from the geocoder.

Why doesn’t it work when I click the submit button but works great from the window.onload?

<script type="text/javascript"
  src="http://maps.googleapis.com/maps/api/js?key=MYSECRETKEYHERE&sensor=false">
</script>    

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>


<form id="update_info" method="post" action="{{upload_url}}" enctyp="multipart/form-data">
<input name="address" id="address" type="text" value="100 W Main St Lehi, UT"/>
<input name="latlng" id = "latlng" type="text" value=""/>
<input name="update_reseller_button" id="update_reseller_button" type="submit">
</form>


<script>

function getLatLng()
{
    var geocoder = new google.maps.Geocoder();

    var theaddress = document.getElementById('address').value;
    var position = new google.maps.LatLng();

    geocoder.geocode({'address': theaddress}, function(results, status
      )
    {                       
        position = results[0].geometry.location;                  
        document.getElementById('latlng').value = position.toString();
    });
    return position;
}

$('#update_info').submit(function()

  {
    alert('The Position is: ' +  getLatLng().toString());
    return true;
  });

  window.onload = getLatLng();

</script>

EDIT: Fixed typo in the handler pointing to the wrong form

EDIT 2: Fixed more typos

EDIT 3: Changed so alert shows the value of position

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:17:20+00:00Added an answer on June 7, 2026 at 9:17 am

    your getLatLng-function will never work as expected, it will never return the result of the geocoding, because geocoding is an asynchronous request.

    This:

    window.onload = getLatLng();
    

    …will call the function(not onload, immediately), and the function will execute the instructions, but will also return (NaN, NaN)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript function from which I need to call a controller action
Want to run javascript function from parent window in child window Example I have
I use the following Javascript function.It will not work.It will give the Undefined Error
My Javascript function aa() is called with the value of the paying_now input field.
I have a javascript function. On-click will call this. Using document.getElementById I am getting
This javascript function takes JSON and formats it to XML. The data is a
My javascript function looks like this - function updateSuccess(aEle) { //Here aEle doesn't have
What Javascript function is used to check for a certain plugin? Such as for
I have a small javascript function which opens an url in a new tab:
I have this Javascript function that sends username and password to php file through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.