javascript - How do I get value from Ajax function for jquery onclick function? -


this question has answer here:

i have button when click on , data database , , display on text area based on id.

jquery

$('#verifybtn').on("click", function() {    var exeoutput = checkoutput();    $("outputarea").html(exeoutput); });  function checkoutput(){     var exno = parseint($('#selectexercise').val());     datastring = "exno=" + exno;     $("#result").empty();     getoutput(datastring, true); }  function  getoutput(datastr, flag) {   $.ajax({       url: "/fyp/webexercisebyoutput",       data: datastr,       success: function (data) {          return data;       },       error : function (xhr,textstatus,errorthrown){           console.log("something wrong ajax call."+ xhr);       }   }); } 

through servlet getting database.

servlet

exercisesmodel outputmodel = null; try {     dbconnection db = new dbconnection();     outputmodel  = db.getexercisebyid(request.getparameter("exno")); } catch (exception e) {     // todo auto-generated catch block     e.printstacktrace(); } response.getwriter().append(outputmodel.getexpoutput()); 

edit: upon clicking, think there data text area not displaying it.

since you're making asynchronous call can't return immediate response using return data;, instead pass selector assign result output element when success callback called:

<script>     $('#verifybtn').on("click", function() {         checkoutput("outputarea");     });      function checkoutput(output_selector){         var exno = parseint($('#selectexercise').val());         datastring = "exno=" + exno;         $("#result").empty();          getoutput(datastring, true, output_selector);     }      function getoutput(datastr, flag, output_selector) {         $.ajax({             url: "/fyp/webexercisebyoutput",             data: datastr,             success: function (data) {                 $(output_selector).html( data );             },             error : function (xhr,textstatus,errorthrown){                 console.log("something wrong ajax call."+ xhr);             }         });     } </script> 

note : passed flag parameter isn't used.

hope helps.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -