Change form action to select options

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

Hi guys,

<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 need a form action to use the value of the select option. For example:

If option 1 is selected, use the following form action / stores / store6.php

+3


source to share


4 answers


You can use event onchange

to change the action of the form

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

      



Here's a jsfiddle with code.

+9


source


<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 want it to work.

+2


source


Add to function onchange

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

      

and add to javascript

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

      

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

0


source


Add this to your selection:

OnChange = "changeAction (this)"

and this to your javascript

changeAction = function(select){
   document.getElementById("store").action = select.value;
}

      

0


source







All Articles