I’m trying to set up a text input box on a webpage that sets a JavaScript variable, however I can’t seem to figure it out. Google hasn’t helped, I’ll I’ve found from my searches was how to set an input box’s text using javascript, which is the opposite of what I need.
Here’s what I’ve got:
window.onload = function() {
document.getElementById('submitButton').onclick = function() {
var searchterm;
searchterm = "error";
searchterm = document.getElementById("searchbox");
};
};
What I want this code to do: Take the content of the input box with the ID “searchbox” whenever I hit “submitButton” and use it as the variable “searchterm”, running all of my code over again with the new variable.
Any help would be much appreciated.
You need to get the
valuefrom the search box. Try something like this:If you need to use the variable
searchtermoutside the scope of the onclick function, just move thevar searchtermdeclaration outside of that function.