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 1069781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:28:18+00:00 2026-05-16T20:28:18+00:00

Look this code: <input type=hidden id=finalvalue value=2 /> <script> $.post(/increment_in_one, { param: 2 },

  • 0

Look this code:

<input type="hidden" id="finalvalue" value="2" />

<script>
    $.post("/increment_in_one", { param: 2 }, function (data) {
        alert( data ); // this shows: 3
        $("#finalvalue").val( data );
        alert( $("#finalvalue").val() ); // this shows 3
    });

    alert( $("#finalvalue").val() );   // this shows 2
</script>

Why does the last alert show 2?

  • 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-16T20:28:18+00:00Added an answer on May 16, 2026 at 8:28 pm

    Because the code doesn’t run in sync as you’d expect from the coding. The $.post call executes asynchronously “in the background”. As soon as you call $.post, the webbrowser will spawn a new thread which does all the task and the current thread just immediately continues with the remnant of the function, which is the alert() which displays 2. The new background thread in turn connects the webserver, fires a HTTP POST and then finally invokes the callback function. It takes more time than the initial thread needs to call the alert(). That’s why you see the difference.

    In this particular code example, you should have noticed that by seeing 2 being alerted before 3. Try adding more sensible information to the alert, e.g.

    alert('Inside $.post callback: ' + $("#finalvalue").val());
    

    and

    alert('At end of function: ' + $("#finalvalue").val());
    

    All with all, if you want to do some stuff based on the new data, the code should then go inside the callback function.

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

Sidebar

Related Questions

look at this code, <head> <meta http-equiv=Content-Type content=text/html; charset=utf-8 /> <script> function change() {
Given the following sample code: $(document).ready(function(){ $(:input).blur(function(){ alert(The input type is: ); //How would
take a look at this example code: public class Comment { private Comment() {
Edit: Of course my real code doesn't look exactly like this. I tried to
Please look at this code (and forgive the lack of knowledge). It outputs errors
The default decorator for the Zend_Form_Element_Radio is <label for=type_id-1><input type=radio name=type_id id=type_id-1 value=1>Pack</label> The
Look at this image: alt text http://img139.imageshack.us/img139/4488/picture2ep3.png I know how to add UITableView with
Look at this situation: www.websitea.com displays an img tag with a src attribute of
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
I was having a look at this tutorial at Sun on command line I/O.

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.