I have a form like this:
JavaScript
x
5
1
<form action="http://localhost/test">
2
<input type="text" name="keywords">
3
<input type="submit" value="Search">
4
</form>
5
If I type a value, let’s say: ‘hello’ in the text input field and submit the form, the URL looks like: http://localhost/test/?keywords=hello
.
I want the value to get appended to the action path. So basically, after the form submission the URL should look like:
JavaScript
1
2
1
http://localhost/test/hello
2
Advertisement
Answer
You can use onsubmit
attribute and set the action inside a function for example:
JavaScript
1
12
12
1
<form id="your_form" onsubmit="yourFunction()">
2
<input type="text" name="keywords">
3
<input type="submit" value="Search">
4
</form>
5
6
function yourFunction(){
7
var action_src = "http://localhost/test/" + document.getElementsByName("keywords")[0].value;
8
var your_form = document.getElementById('your_form');
9
your_form.action = action_src ;
10
}
11
12