How to change the form action before submit on the basis of element/input field click.
The html sample code for form having radio button and submit button :
<form action="" id="myformID">
<input type="radio" name="changeaction" value="male" id="ifmale"/>Male<br>
<input type="radio" name="changeaction" value="female" id="iffemale">Female<br>
<button type="submit" form="form1" value="Submit">Submit</button>
</form>
The script code to change the form action button on click of radio button :
<script type="text/javascript">
$('#ifmale').click(function(){
$('#myformID').attr('action', '/abc.php');
});
$('#iffemale').click(function(){
$('#myformID').attr('action', '/xyz.php');
});
</script>
You can customize the above script for any input field like select, text, checkbox etc.
First match the value on click event of the element and then change the action attribute of the form by passing form's id.
0 Comment(s)