This is driving me up the wall, I have tried various solutions to try and get the input into a text box to store to a variable on submit.
My current code is:
function getInput() {
var userInput = document.getElementById('userInput').value;
}
var subButton = document.getElementById('subButton');
subButton.addEventListener('click', userInput, false);
It looks like you are calling an undefined function called userInput. The function you defined is getInput. Also, to make sure it works, after you get the value, you can log it to the console like this:
Thanks for the response. Yes you’re right it should be:
function getInput() {
var userInput = document.getElementById('userInput').value;
}
var subButton = document.getElementById('subButton');
subButton.addEventListener('click', getInput, false);
But unfortunately my issue remains in that, all I get returned is the element and not the value entered in to the text box?
Did you make sure you access the value property? I see it’s in your code but are you actually typing something in the console? Like I actually said in the previous post, you can log it to the console directly in your code like this:
function getInput() {
var userInput = document.getElementById('userInput').value;
console.log(userInput);
}
var subButton = document.getElementById('subButton');
subButton.addEventListener('click', getInput, false);
Oops, I was typing that at the same time… that response doesn’t quite make sense anymore for a reply to that. If you want to store it globally, instead of saying var userInput = value, you can say window.userInput = value. Then you can use that anywhere
The way you have it now, when the document loads, searchQ will be assigned to searchBox's value, which would be nothing at that time, but that is not what you want, you want searchQ to be assigned the value when the user clicks, your click event triggers the userInput() function, that’s when you want to get the value of the search box, so, move your assignment inside but before the fetch of the userInput() function, that way, as soon as the user clicks the searchBox‘s variable is attached to searchQ , then you can do your fetch, btw your api endpoint ain’t workin’, i don’t think…