I'm trying to get the text typed inside of an input element to be usable for a JSON request.
$("document").ready(function() {
var search = $('#input').val()
var api =
"https://en.wikipedia.org/w/api.php?callback=?&action=opensearch&format=json&search=" +
search +
"&namespace=0&limit=10&profile=fuzzy&suggest=1";
// Button function
$(".btn").click(function() {
function listClear() {
$("ul").empty();
}
listClear();
$.getJSON(api, function(data) {
function listCreate() {
var arr = data[1];
for (i = 0; i < arr.length; i++) {
$("ul").append("<li>" + arr[i] + "</li>");
}
}
listCreate();
});
});
});
and my element looks like this
<input type="search" id="input" class="form-control" placeholder=". . .">
When I do it like this, the console shows an error saying that the length of undefined cannot be found. I get that this means my current value retrieval method is not working. Any ideas?
ps. I'm also doing this coding on codepen (https://codepen.io/WatchConnorCode/pen/JybGVJ?editors=1010) I don't know if that would cause issues?
edit: I had it at val previously and forgot to change it back. It still is not working as intended.