Hello all, I'm new to this and have been searching the internet high and low to see if anyone has encountered a similar issue before, but haven't had any luck.
I've been attempting to set up a JSONP request to Wikipedia that is connected to a user input field on my webpage. This request should be triggered by a button click in a search field. Everything works smoothly the first time it's used after the page loads (returns the numerical page id), but subsequent searches don't update the response variable...
Thank you in advance for your assistance!
P.S. - I'd prefer to avoid using jQuery if possible and stick with pure JS, thank you.
Find the code below:
var x = document.getElementById("test");
var y = document.getElementById("loadResults");
var wikiResponse;
var script = document.createElement("script");
var userInput;
function launchSearch () {
if (document.body.contains(script)) {
document.body.removeChild(script);
}
script.src = "https://en.wikipedia.org/w/api.php?action=query&titles=" + userInput + "&prop=revisions&rvprop=content&indexpageids=1&format=json&callback=loadResults";
document.body.appendChild(script);
}
function loadResults(response) {
wikiResponse = response.query.pageids[0];
x.textContent = wikiResponse;
}
y.addEventListener("click", function() {
userInput = document.getElementById("userInput").value;
launchSearch();
});