javascript child function pass the return to the parent function return [duplicate]


This question already has an answer here:

<ul><li> <a href="/questions/1225667/how-to-return-ajax-response-text" dir="ltr" rel="nofollow">How to return AJAX response Text? [duplicate]</a> <span class="question-originals-answer-count"> 2 answers </span> </li> <li> <a href="/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call" dir="ltr" rel="nofollow">How do I return the response from an asynchronous call?</a> <span class="question-originals-answer-count"> 33 answers </span> </li> </ul>

i want to pass the return from a "child" (i don't know the correct term) function to the return in the parent function... how is the correct way to do this? check_data() isn't returning true or false

function check_data(type,field){ var chkvalue = $(field).val(); $.post("mods/ajax.fieldchk.php", { chkvalue: chkvalue, type: type }, function(result){ if(result==0){ $(field).css({'background-color': '#faa', 'border': '1px solid #f00'}); return false; }else if(result==1){ $(field).css({'background-color': '#afa', 'border': '1px solid #0f0'}); return true; } }; }



Since $.post is an asynchronous function you can't use it to return data on check_data. However you can pass some callback and execute with true/false argument.

Something like this:

function check_data(type,field,callback){ var chkvalue = $(field).val(); $.post("mods/ajax.fieldchk.php", { chkvalue: chkvalue, type: type }, function(result){ if(result==0){ $(field).css({'background-color': '#faa', 'border': '1px solid #f00'}); callback(true); }else if(result==1){ $(field).css({'background-color': '#afa', 'border': '1px solid #0f0'}); callback(false); } }; }


  • Cassandra: What is a subcolumn
  • How can I count unique terms in a plaintext file case-insensitively?
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • Image map in Flex
  • Web.config system.webserver errors
  • Force show.bind execution
  • How can the INSERT … ON CONFLICT (id) DO UPDATE… syntax be used with a sequence ID?
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Allowing both email and username for authentication
  • Disable Enter in editText android
  • Retrieving value from sql ExecuteScalar()
  • ORA-29908: missing primary invocation for ancillary operator
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • 0x202A in filename: Why?
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to disable jQuery.jplayer autoplay?
  • How to delete a row from a dynamic generate table using jquery?
  • KeystoneJS: Relationships in Admin UI not updating
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • C# - Getting references of reference
  • using HTMLImports.whenReady not working in chrome
  • Hits per day in Google Big Query
  • How do I configure my settings file to work with unit tests?
  • How to stop GridView from loading again when I press back button?
  • File not found error Google Drive API
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • Bitwise OR returns boolean when one of operands is nil
  • EntityFramework adding new object to nested object collection
  • sending mail using smtp is too slow
  • Checking variable from a different class in C#
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?