I have a form in my page like the one below;
<form name="testform" id="testform" action="test.php" method="get">
<input name="field1" id="field1" type="text" value="">
<input name="field2" id="field2" type="text" value="">
<select name="dropdown" id="dropdown">
<option value="option1" selected="selected">option1</option>
<option value="option2">option2</option>
<option value="option3">option3</option>
</select>
<input type="submit" name="Submit" value="Submit" id="Submit">
</form>
I want the form to get submitted automatically when user select an option from the drop-down menu. How can I do this with or without using JavaScript (with or without jQuery)?
Thanks in advance... :)
blasteralfred
Click (or select)? In that case the user would not be able to make any selection. You probably mean as soon as another option is selected. If so
<select name="dropdown" id="dropdown" onchange="this.form.submit()">
If jQuery is being used, unobtrusive event handler change should be used instead of inline javascript.
$(function(){
$("#dropdown").change( function(e) {
this.form.submit();
});
});
Use on if the form elements are dynamically being added in the DOM
$(function(){
$('#testform').on( "change", "#dropdown", function(e) {
this.form.submit();
});
});
You will need to use the jQuery change() event.
('#dropdown').change( function() { ('#testform').submit(); })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With