10

I want to change the form action based on a selection value.

<form name="store" id="store" method="post" action="">
<select name="storeID">
<option value="/stores/store6.php">6</option>
<option value="/stores/store10.php">10</option>
</select>                   
</form>

Now I want the form action, to use the select option value. For example:

If Selection 1 is selected, use the folowing form action /stores/store6.php

peterh
  • 11,875
  • 18
  • 85
  • 108
daniel.
  • 128
  • 1
  • 1
  • 10

4 Answers4

14

You can use the onchange event to change the form's action

document.getElementById('store').storeID.onchange = function() {
    var newaction = this.value;
    document.getElementById('store').action = newaction;
};

Here is a jsfiddle with the code.

Tasos K.
  • 7,979
  • 7
  • 39
  • 63
2

Add to select onchange function

<select name="storeID" onchange='changeAction(this.value)'>

and add to javascript

function changeAction(val){
    document.getElementById('storeID').setAttribute('action', val);
}

This will change action after selected option is changed to selected option value.

Jozef Dúc
  • 965
  • 2
  • 18
  • 29
2
<form name="store" id="store" method="post" action="" id="FORM_ID" >
    <select name="storeID">
        <option value="/stores/store6.php">6</option>
        <option value="/stores/store10.php">10</option>
    </select>                   
</form>

<script type="text/javascript">
document.getElementById('storeID').onchange = function(){
    document.getElementById('FORM_ID').action = '/'+this.value;
}
</script>

check it out i wish it will work ..

Ferrakkem Bhuiyan
  • 2,741
  • 2
  • 22
  • 38
1

Add this to your select:

onchange="changeAction(this)"

and this to your javascript

changeAction = function(select){
   document.getElementById("store").action = select.value;
}
Johan Karlsson
  • 6,419
  • 1
  • 19
  • 28