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

  • SEARCH
  • Home
  • 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 310113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:48:13+00:00 2026-05-12T07:48:13+00:00

I thought I had this mess sorted out in my head but for some

  • 0

I thought I had this mess sorted out in my head but for some odd reason its not working.

If you declare a variable outside of a function / scope and refer to it without the var inside a function then it changes the variable declared previously… right?

however, the first alert returns the correct price, but the second (last) alert returns 0. What am I doing wrong?

//get pricing
var price=0;
var modelid = $("#model_input").val();
var inCode = $("#code_input").val();
$.get("getpricing.php", {  'modelid': modelid ,'code' : inCode }, function(data){
    price = data;
    alert(price);
});
alert(price);
  • 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-05-12T07:48:14+00:00Added an answer on May 12, 2026 at 7:48 am

    You are using an Ajax request.

    Those, unkess specified otherwise, are Asynchrounous : they are executed in the background, without stopping the execution of the rest of the script.

    So, the alert on the last line of code is executed before the Ajax request is finished ; which means price is still 0 at that time.

    One way to change that would be to use a synchronous request (see async option) ; but I strongly advise against it ; quoting the doc :

    By default, all requests are sent
    asynchronous (i.e. this is set to true
    by default). If you need synchronous
    requests, set this option to false.
    Note that synchronous requests may
    temporarily lock the browser,
    disabling any actions while the
    request is active.

    And you definitly don’t want your application to freeze the whole browser!

    You should re-think the way your application is designed, in this case : you can only use the “price” information after the Ajax request is finished — which probably means you should place more code inside the function called on its success : just placing code after the $.get is not enough.

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

Sidebar

Related Questions

I thought I had this perfectly working but apparently not. I left my project
I thought I had this figured out, but can't get it working correctly: All
I thought I had this figured out but it turns out I'm just deleting
I thought I had this all figured out, but I'm hitting a brick wall.
I thought I had this figured out, but I'm running into an issue. I'm
I thought I had this sorted, but I am still missing something. Very simply,
I thought I had this all figured out, but now that I'm writing a
I thought I had this figured out, but I obviously don't. In an Android
I thought I had this figured out, but I'm wanting to find all occurances
Ok, so I thought I had this nailed , but sadly not . I

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.