I created a some jquery code which gets some json data and displays it on the page. In order to reuse it, i wrapped it in a function:
$(function(e) {
var answer;
id = 1;
var question = function() {
$.getJSON('getjson.php?id=' + id, function(data){
console.log(data);
var $htmlString = "";
$.each(data, function(index) {
$htmlString += "<h2>" + data[index].question + "</h2>";
str = data[index].answer;
answer = data[index].answer.split(', ');
$htmlString += "<ul>";
for(var index in answer) {
$htmlString += "<li>" + index + " : " + answer[Math.floor(Math.random() * answer.length)] + "</li>";
}
$htmlString += "</ul>";
});$('#content').html($htmlString);
});
};
});
Now, the first time the page loads, I need that function to run by itself, then when I click on ‘next’ the id increases and the function is triggered.
The above function works great when I use the click trigger, but how can I trigger it right after the document is ready.
Thanks
you can also use: