151

I'm trying to change the form action based on the selected value from a dropdown menu.

Basically, the HTML looks like this:

<form class="search-form" id="search-form" method="post" accept-charset="UTF-8" action="/search/user">
<select id="selectsearch" class="form-select" name="selectsearch">
<option value="people">Search people</option>
<option value="node">Search content</option>
</select>

<label>Enter your keywords: </label>
 <input type="text" class="form-text" value="" size="40" id="edit-keys" name="keys" maxlength="255" />

<input type="submit" class="form-submit" value="Search" id="edit-submit" name="search"/>
</form>

If "people" is selected, (which it is by default), the action should be "/search/user", and if content is selected, the action should be "/search/content"

I'm still searching around, but haven't been able to find out how to do this.

informatik01
  • 16,038
  • 10
  • 74
  • 104
n00b0101
  • 6,863
  • 17
  • 42
  • 36

6 Answers6

277
$("#selectsearch").change(function() {
  var action = $(this).val() == "people" ? "user" : "content";
  $("#search-form").attr("action", "/search/" + action);
});
cletus
  • 616,129
  • 168
  • 910
  • 942
  • That is way better than what I was trying... Thank you – n00b0101 Dec 18 '09 at 00:57
  • 1
    Worked perfect. Thanks. I updated this code to change the action on submit instead of on form change. – Ryan Dec 16 '11 at 01:25
  • 1
    If you design for mobile devices it would be better to change action "on submit" for performance issues. Check my answer below. – trante Dec 13 '13 at 13:13
  • Use `prop` instead of `attr`. Example = `$("#search-form").prop ("action", "/search/" + action); ` – gsinha Feb 14 '16 at 07:30
24

If you only want to change form's action, I prefer changing action on-form-submit, rather than on-input-change. It fires only once.

$('#search-form').submit(function(){
  var formAction = $("#selectsearch").val() == "people" ? "user" : "content";
  $("#search-form").attr("action", "/search/" + formAction);
}); 
Tiny
  • 27,221
  • 105
  • 339
  • 599
trante
  • 33,518
  • 47
  • 192
  • 272
1

Simple and easy in javascipt

<script>

  document.getElementById("selectsearch").addEventListener("change", function(){

  var get_form =   document.getElementById("search-form") // get form 
  get_form.action =  '/search/' +  this.value; // assign value 

});

</script>
Swift
  • 790
  • 1
  • 5
  • 19
0

It's better to use

$('#search-form').setAttribute('action', '/controllerName/actionName');

rather than

$('#search-form').attr('action', '/controllerName/actionName');

So, based on trante's answer we have:

$('#search-form').submit(function() {
    var formAction = $("#selectsearch").val() == "people" ? "user" : "content";
    $("#search-form").setAttribute("action", "/search/" + formAction);
}); 

Using setAttribute can save you a lot of time potentially.

scode102
  • 329
  • 4
  • 6
0

If you want to change from Pure javascript means use follow methods. Same as Ankush Kumar's code with Extra methods

function fn_one(){
  document.formname.action="https://google.com/search?q=form+submit+using+name";
}
function fn_two(){
  document.getElementsByName("formname")[0].action="https://google.com/search?q=form+submit+using+name+method+2";
}
function fn_three(){
  document.getElementById("formid").action="https://google.com/search?q=form+submit+using+ID";
}
<form name="formname" id="formid" action="google.com/search?q=" target="_blank">
  <div>
    <button type="button" onclick="fn_one()">Change By Name {1}</button>
    <button type="button" onclick="fn_two()">Change By Name {2}</button>
    <button type="button" onclick="fn_three()">Change By ID</button>
  </div>
   <div>
    <button type="submit">Go To Google</button>
  </div>
</form>
    
Madhan
  • 107
  • 12
-16

Is required that you have a form?
If not, then you could use this:

<div>
    <input type="hidden" value="ServletParameter" />
    <input type="button" id="callJavaScriptServlet" onclick="callJavaScriptServlet()" />
</div>

with the following JavaScript:

function callJavaScriptServlet() {
    this.form.action = "MyServlet";
    this.form.submit();
}
JustCarty
  • 3,839
  • 5
  • 31
  • 51
Dago Jobs
  • 1
  • 1
  • 15
    this.form.submit(); will have no sense since there are actually no forms an thus - nothing to submit – SET001 Jun 30 '12 at 07:39