Following java script is not working global variable pls help to do. Ajax is working properly and in add_article() function also working as variable but the variable not working undefined.
<script type="text/javascript">
var karat = 0;
function get_advance_amount(element)
{
if (element!="")
{
if (window.XMLHttpRequest)
{
key = new XMLHttpRequest();
key.onreadystatechange = function(){
if (key.status==200 && key.readyState ==4)
{
karat = key.responseText;
//alert(karat); // this is working
}
};
key.open("GET","get_advance_amount.php?q="+element,true);
key.send();
}
}
}
function add_article()
{
get_advance_amount('2');
alert(karat); // this is not working
}
</script>
<button onclick="add_article()">Click me</button>