aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes There is a delay before html textbox value gets updated after ajax function execution Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "There is a delay before html textbox value gets updated after ajax function execution" Watch "There is a delay before html textbox value gets updated after ajax function execution" New topic
Author

There is a delay before html textbox value gets updated after ajax function execution

varun ojha
Greenhorn

Joined: Jun 07, 2012
Posts: 1
I am using jcaptcha for image verification in my form. Before the form is submitted I make an ajax call using javascript to validate the text entered by the user corresponding to the image displayed. I get the result and update the value of a textbox(imageVerification). After the function that makes this ajax call is executed I pick up the value from this just updated textbox(imageVerification) for the result.

Here is the problem: I am not able to pick up the value from this textbox(imageVerification). it always shows up as blank.

Catch: if I use an alert() before picking up the value, I am able to pick up the value correctly. I ran this in firebug debug mode and found out that it works in debug mode even without using the alert.

It seemed there is a delay before which the value in the textbox(imageVerification) gets updated. So i introduced a setTimeout() method and was able to pick up the value.

But I dont feel this is the right solution. I am assuming javascript executes sequentially. So why is my statement which is picking up the value after it has been updated by a method not able to get it immediately. Result is even though the image verification is successfull, my check fails since it is not able to pick up the result value from the textbox.

Also, if I use a simple function to update the textbox(imageVerification) instead of a ajax call, I dont face this problem.

Here is the code I am using for the ajax call.


This is the function for image verification:

post my call to fetchContainerContent('captchaController','imageVerification'), the value for imageVerification textbox should be set. If I use the alert box which is commented after the fetchContainerContent('captchaController','imageVerification') call it works fine.

Please help me out. Thanks alot
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

varun ojha wrote:
post my call to fetchContainerContent('captchaController','imageVerification'), the value for imageVerification textbox should be set.

No, it shouldn't. You are forging ahead before the Ajax request has returned. Any processing that needs to wait for the request to finish needs to be contained in the response callback.

That's why alerts and timeouts cause it to work. Those delay the processing long enough for the request to finish.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

Highly recommend simplifying your code extensively using jQuery.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: There is a delay before html textbox value gets updated after ajax function execution