maudulus
maudulus

Reputation: 11025

Submitting a search is not sending me to the page specified in javascript variable

I am trying to submit a search which should redirect me to the page that I want on submission. However, submitting the form just redirects me to the index page.

<form class="searchbox" onsubmit="gotToCorrectSearchPage();" ><input id="tags" type="search" placeholder="Search"/></form>

The Javascript function, goToCorrectSearchPage, which is called onsubmit, is below. The alert works, but then I am redirected to the index page. Note: goToSearchPage is a variable that has been set somewhere else. If I simply call window.location=goToSearchPage; in the javascript console it takes me to the correct page.

function gotToCorrectSearchPage() {
    alert("directing you");
    window.location=goToSearchPage;
}

Thus, I need a way to call this window.location=goToSearchPage; correctly inside of the goToCorrectSearchPage function such that it will load the page saved in the goToSearchPage variable, and not load index page. I have tried a number of default action preventions on the form inside of the goToCorrectSearchPage function, but to no avail.

Thanks!

Upvotes: 0

Views: 45

Answers (1)

epascarello
epascarello

Reputation: 207501

The form is submitting and refreshing the page. You need to cancel it.

onsubmit="gotToCorrectSearchPage(); return false"

Ideally you would use preventDefault or set the action and let the form do its thing.

Upvotes: 1

Related Questions