javascript - JSONP request linked to user input field+button: only executes once -
beginner here, been scouring web trying find whether question has been asked before, no luck.
i've been trying create jsonp request wikipedia linked user input field on page, activated button i.e. search field. fires off fine first it's used after page load (returns numerical page id), thereafter doesn't overwrite response variable if try search again...
thanks in advance help!
ps - preferably avoid using jquery if possible , stick pure js, thanks.
code here:
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(); });
the script creating executed if created createelement
, not if removed body , added again using appendchild
(see this answer).
instead, try creating element (using createelement
) every time want load instead of removing element dom , re-adding again.
Comments
Post a Comment