Thiago Dias
Thiago Dias

Reputation: 335

Get input value and redirect

how to get the text input from the form and redirect it to antother page, using js?

<form action="">
<input type="text" name="query" id="query" />
<input type="submit" value="search" />
</form>

and, on submit, redirect to something like this:

site.com/search/ INPUT_TEXT_HERE

Upvotes: 0

Views: 2037

Answers (2)

roshan
roshan

Reputation: 1

<form action="place ur URL/>; and write your code.when you click on submit.it will be redirected to some given link.

Upvotes: 0

George Pant
George Pant

Reputation: 2117

Just use jquery's val() to get the value of your input and window.location.replace to redirect to the url you want.

Finally execute your code when the form is submitted using jquery's submit function

In order to prevent the form from submitting you should use jquery's preventDefault()

$("form").submit(function(e){
e.preventDefault();
window.location.replace("site.com/search/"+$("#query").val());
}) 

Upvotes: 1

Related Questions