I have an HTML form that I am trying to use to navigate to a different page. I was trying to use window.location.replace
to append the value of the input to the end of the form like this:
Before: https://example.com/search
I have tried just about every trick I can find, but have had no luck. I was able to get it working by replacing window.location.replace
with window.open
, but I don't want to have to open it in a new tab. I also tried window.location.assign
but had no more luck with that. I tried running both of these functions in the Chrome console and they worked fine from there. My code is below.
function onenter() {
var term = document.getElementById("searchbox").value;
window.location.replace("/search/" + term);
}
<form method="GET" onsubmit="onenter();">
<input id="searchbox" name="term" type="text" autofocus>
<button id="searchenter" type="submit">Enter</button>
</form>
What am I doing wrong/missing?