I am having problem in displaying the nested objects within the JSON structure into the a page using JQuery. I'm using JQuery's function (.getJSON), but it doesn't seem to be working.
This is the JSON file below:
{
"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
"text": {
"data": "Click Here",
"size": 36,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}
}
And this is the Javascript file that uses JQuery to access the JSON objects:
$(document).ready(function () {
$('#letter-w a').click(function (event) {
event.preventDefault();
$.getJSON('widget.json', function (data) {
var html = '';
html += data.widget.debug;
html += data.widget.window.title;
html += data.widget.window.name;
});
$('#output').html(html);
});
});
In the above code, #letter-w is a id for a link that when clicked displays the result and #output is the div where the output/results will be displayed within the HTML page itself. Also, for the sake of checking, I've only written 2-3 lines to access the JSON objects. P.S JSON is very confusing since you've to takecare of all the braces and all. Any suggestions or help would be grateful. Thanks in-advance!
#letter-wis an anchor, but you're selecting#letter-w awhich will look for an anchor within#letter-w.