Reputation: 11
I am trying to use an JSON string to popolate an html div, here's my code, but it seems not working, I still cannot realize why, there's some suggestions?
The JSON is created via PHP and it is working fine. Thank you.
$(document).ready(function(e) {
var Currencies = function(){
$.ajax({
url:"js/php/users.php",
success:function(data){
return{
getMyJson: function(data){
return(data);
}
}
}
});
}(); // execute the function when the MyObj variable is initialized.
});
$('#autocomplete').autocomplete({
lookup: Currencies.getMyJson(),
onSelect: function (suggestion){
var thehtml = '<strong>Currency Name:</strong> ' + suggestion.nome + ' <br> ';
$('#outputcontent').html(thehtml);
}
});
Upvotes: 1
Views: 81
Reputation: 388406
Few observations
Currencies
object is local to the dom ready handler, so it is not accessible outside it. Move your autocomplete
plugin initialization inside the dom ready handler.Try
$(document).ready(function (e) {
var Currencies = {
getMyJson: function (callback) {
$.ajax({
url: "js/php/users.php",
success: function (data) {
callback(data)
}
});
}
}; // execute the function when the MyObj variable is initialized.
Currencies.getMyJson(function (data) {
$('#autocomplete').autocomplete({
lookup: data,
onSelect: function (suggestion) {
var thehtml = '<strong>Currency Name:</strong> ' + suggestion.nome + ' <br> ';
$('#outputcontent').html(thehtml);
}
});
})
});
Upvotes: 1
Reputation: 24384
move this code
$('#autocomplete').autocomplete({
lookup: Currencies.getMyJson(),
onSelect: function (suggestion){
var thehtml = '<strong>Currency Name:</strong> ' + suggestion.nome + ' <br> ';
$('#outputcontent').html(thehtml);
}
});
into
$(document).ready(function(e) {
Upvotes: 0