I am using Javascript to bring in a json list of races that have been completed from an external URL. There could be 1 race completed or a hundred. You click the button and it populates the list. Once this is done, I need to be able to click one of the race buttons and have it pull in another json list from an external URL and shows the list of runners and their times.
My issue is how to use Javascript to make any of the possibly 1 to 100 buttons clickable to bring in the data from an external website. If I click the first button, it would bring in data from website.com/1. If I click the second, it brings the data from website.com/2, and so on.
Here is the code I have so far:
$("#getList").unbind("click").click(function() {
$.getJSON("myURL.com/races/", dataFunc);
function dataFunc(data) {
$jsonData = data;
displayRaces($jsonData.races.length, $jsonData)
}
});
function displayRaces(length, json) {
console.log("inside display races function");
i = 0;
while (i < length) {
$race = json.races[i].race_name;
var mydiv = document.getElementById("getResults");
var aTag = document.createElement('BUTTON');
var linkURL = "#" + i;
aTag.setAttribute('id', linkURL);
aTag.setAttribute('href', linkURL);
aTag.innerHTML = $race;
mydiv.appendChild(aTag);
i++;
}
}
This works to make a list of the buttons for the races. Now I need to figure out how to make it so when I click the button, it can essentially run a similar code to display the runners. I was thinking of somehow making it so that there is a variable in
$("#variable").unbind("click").click(function() {}
line so that you can say "If I click the button #1, get the json data from myurl.com/data/1 and display it"
event.targetorthis. developer.mozilla.org/en-US/docs/Learn/HTML/Howto/…