I have a js function that use $.post to get data and store value in hidden field in the function it work fine but outside the function after calling function it won’t work but if i use just alert(‘sometext’); before the alert the value of hidden field then it will store value and give correct output
code look like
function abc(){
$.post('sch.php',{begin:'he'},function(data){
$('#g').attr('value',data);
});
}
//call a function
abc();
alert($('#g').attr('value'));
//won't alert stored value
$.post is asynchronous. data is not available while you trying to acceess it.You should use synchronous call or put all depended code inside callback method.Modified code: