function to return value from ajax call
I asked something similar before, but my code was pretty confusing so I rewrote the whole thing and asking a more direct question.
Anyway, I have a function that simply makes an ajax call that should return a css property from a database. Then I want to take that returned property and apply it to the page. So my function goes something like this:
-
function loadAttribute(attribute) {
var value = '_blank_';
$.post("db_output.php", "attribute=" + attribute, function(data) { alert('data from ajax: ' + data); value = data; });
alert('actually returned value: ' + value);
return value;
}
the function creates a variable named 'value', runs a post ajax query which should set the value of the 'value' variable to data returned from the query and then returns the variable 'value'. Pretty simple. However, for whatever reason, the callback function never sets the value of 'value' to the returned data.. I don't know what I've missed and pages upon pages of google search results came up with nothing to help me. So how can I change my code to get it to work?
Basically I want to be able to call this function like
-
$('body').css('backgroundColor', loadAttribute('bg_color'));
and for it to work.. Any ideas?
btw, here's a test page.. all the code is in the head section of the page
http://4funzies.host22.com/manage/test.php