html - Changing form action based on select value - with JavaScript - Stack Overflow
I have a form that I wish to change the action of, depending on what value a user selects in a drop down box.
The example code below shows each option value, I want to change the form action to whatever option value is chosen on a button press, how can I do this with JavaScript?
<form name ="form1" action="VariableFormAction" method ="post" target="_blank">
<select name="formchoice">
<option value="/formaction1">function 1</option>
<option value="/formaction2">function 2</option>
<option value ="/formaction3">function 3</option>
</select>
<input type="submit" value="Go" class="button">
</form>
I have a form that I wish to change the action of, depending on what value a user selects in a drop down box.
The example code below shows each option value, I want to change the form action to whatever option value is chosen on a button press, how can I do this with JavaScript?
<form name ="form1" action="VariableFormAction" method ="post" target="_blank">
<select name="formchoice">
<option value="/formaction1">function 1</option>
<option value="/formaction2">function 2</option>
<option value ="/formaction3">function 3</option>
</select>
<input type="submit" value="Go" class="button">
</form>
Share
Improve this question
asked Mar 11, 2018 at 15:01
SamoliverczSamolivercz
2201 gold badge4 silver badges11 bronze badges
1
- 1 Possible duplicate of How to set form action through JavaScript? – Matt Healy Commented Mar 11, 2018 at 15:04
3 Answers
Reset to default 2In order to do this you need to specify an onSubmit JavaScript event handler on the form that retrieves the value of the select list and updates the form action.
You can do this using the following code.
//Add onSubmit() event handler to form to call JavaScript method when the form is submitted.
<form name ="form1" onSubmit="actionOnSubmit()" method ="post" target="_blank">
<select id="formchoice" name="formchoice">
<option value="/formaction1">function 1</option>
<option value="/formaction2">function 2</option>
<option value ="/formaction3">function 3</option>
</select>
<input type="submit" value="Go" class="button">
</form>
<script>
function actionOnSubmit()
{
//Get the select select list and store in a variable
var e = document.getElementById("formchoice");
//Get the selected value of the select list
var formaction = e.options[e.selectedIndex].value;
//Update the form action
document.form1.action = formaction;
}
</script>
Note, for this to work you will need to make sure the select list has an id as you are using the document.getElementById[] JavaScript method to retrieve the value of the control.
You could also call the JavaScript in the OnChange() event of the control. The issue with this is that it implies that the value has changed before the form is submitted. If the user simply left the default select list value it is possible that the OnChange() event would never fire.
You can try like following.
function selectChanged(ctrl) {
var val = ctrl.value;
var frm = document.getElementById('form1');
frm.action = val;
}
<form id="form1" name="form1" action="/formaction1" method="post" target="_blank">
<select name="formchoice" onchange="selectChanged(this)">
<option value="/formaction1">function 1</option>
<option value="/formaction2">function 2</option>
<option value ="/formaction3">function 3</option>
</select>
<input type="submit" value="Go" class="button">
</form>
Use on change attribute.
<select name="forma" onchange="location = this.value;">
<option value="Home.php">Home</option>
<option value="Contact.php">Contact</option>
<option value="Sitemap.php">Sitemap</option>
</select>
- 谷歌的成功与失误:安卓迎变化眼镜前途未卜
- 力压谷歌、苹果,微软凭什么成为软件霸主?
- Intel毫不客气:ARM+Win8软硬件都不行
- How to upload wordpress from local to Godaddy wordpress hosting? - Stack Overflow
- react native - Handling loginlogout without user undefined errors - Stack Overflow
- tracking - Add custom hand gestures in three.js - Stack Overflow
- linux - Unable to access devttyS0 as a regular user, despite having the right permissions - Stack Overflow
- javascript - AJAX implementation doesn't work properly for "like" button ASP.NET Core 2.1 - Stack Over
- python - testing the output of seaborn figure level plots - Stack Overflow
- How to make smooth animations in Android Compose for expanding TextField? - Stack Overflow
- android - startActivity() on external application not bring it foreground in task of testing external application - Stack Overfl
- smartcontracts - FailedCall() with OpenZeppelin meta transactions (ERC2771Forwarder and ERC2771Context) - Stack Overflow
- apple swift: is forKey:fileSize considered accessing non-public API? - Stack Overflow
- dolphindb - Whitepaper error: “cannot recognize the token b” - Stack Overflow
- html - django multi-level template extending - Stack Overflow
- javascript - What are the benefits of Next.js Server Actions, and why should I use them instead of client-side API calls? - Stac
- reactjs - Google Books API setOnLoadCallback works only after page reload - Stack Overflow