I have created a variable and tried to set its value by a function. But this value in not accessible outside that function
<script type="text/javascript">
$(document).ready(function () {
var pId;
getProfiles(id,function(b)
{
pId=b;
alert(pId); // It works fine...
});
alert(pId); // undefined...
});
</script>
The first alert works because it is just after the value is set by the callback function you’re passing to
getProfiles.The second alert in your code example still shows the value “undefined” because the callback function you are passing to getProfiles hasn’t executed yet.
Also, just a side note:
pIdis local to the function you are passing to jQuery’s ready event, it is not a global variable.