I am querying an API that I made for my site which returns data in this format:
{
"success":true,
"code":1,
"quantity":3,
"classes":[
[
{
"name":"ECEC 301 Advanced Programming for Engineers Lab",
"days":"W",
"times":"09:00 am - 10:50 am",
"crn":"11216"
},
{
"name":"ECEC 301 Advanced Programming for Engineers Lecture",
"days":"T",
"times":"02:00 pm - 03:20 pm",
"crn":"11215"
},
{
"name":"ECEC 302 Digital Systems Projects Lab",
"days":"MW",
"times":"03:00 pm - 04:50 pm",
"crn":"12567"
}
],
[
{
"name":"ECEC 301 Advanced Programming for Engineers Lab",
"days":"W",
"times":"09:00 am - 10:50 am",
"crn":"11216"
},
{
"name":"ECEC 301 Advanced Programming for Engineers Lecture",
"days":"T",
"times":"02:00 pm - 03:20 pm",
"crn":"11215"
},
{
"name":"ECEC 302 Digital Systems Projects Lab",
"days":"TR",
"times":"09:00 am - 10:50 am",
"crn":"13523"
}
],
[
{
"name":"ECEC 301 Advanced Programming for Engineers Lab",
"days":"F",
"times":"02:00 pm - 03:50 pm",
"crn":"11217"
},
{
"name":"ECEC 301 Advanced Programming for Engineers Lecture",
"days":"T",
"times":"02:00 pm - 03:20 pm",
"crn":"11215"
},
{
"name":"ECEC 302 Digital Systems Projects Lab",
"days":"MW",
"times":"03:00 pm - 04:50 pm",
"crn":"12567"
}
],
"message":"3 schedule[s] were generated"
}
When the page loads, a panel queries this API like so:
$.ajax({
url: '{{ URL('schedulizer/schedules') }}',
type: "GET",
dataType: 'json'
}).done(function(data){
// Get the hash
window.location.hash = '#1';
var type = window.location.hash.substr(1);
$("#classes").html(JSON.stringify(data.classes[type]));
});
What it does is, it gets the URL hash (so #1 for example), and uses that as the index for the classes array from the JSON API. A query could contain many schedules, or no schedules at all.
I have some test HTML which are essentially buttons with a different hash value from the current one. I'm trying to see if I could cycle through the different indices of the JSON array:
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">Schedule</h3>
</div>
<div id="classes" class="panel-body panel-options">
<div class="btn-group">
<a href='{{ URL('schedulizer/schedule#2') }}' class="btn btn-default"><</a>
<a href='{{ URL('schedulizer/schedule#3') }}' class="btn btn-default">></a>
</div>
</div>
</div>
And the page looks like this. As you can see, there's one index of the JSON array, and the button.
The issue:
When I click the button, I see that the URL hash changes from #1 to #2 to #3, however, I do not see that the JSON changes.
The data returned from the classes object are arrays of arrays. I want to loop through those arrays of arrays using the buttons without reloading the page, and subsequently updating #classes
I'm pretty new to Javascript and asynchronous programming, so any help is much appreciated.
