Reputation: 207
I am having a form
where the fields need to change according to my select
.
But when I hit the reset
the select
resets back to default, but the onchange
event on the select
is not triggered. Is there anyway so that I can add that to my javascript?
I am resetting using a button with type="reset"
$('#newHistoryPart select[name="roundType"]').on('change', function (data)
{
$(".answerType").hide();
selected = $(this).find("option:selected").val();
roundTypeChange(selected);
});
Upvotes: 8
Views: 12171
Reputation: 364
What you need to do is, trigger the change event manually when the reset button is clicked. See Fiddle here
$('select').on('change', function ()
{
alert('on change');
});
$('input[type="reset"]').click(function() {
$("select").trigger('change');
});`
Upvotes: 7
Reputation: 5810
This may help you, replace alert lines with your activity code.
HTML
<select name="opt" onchange="getval(this)">
<option value="Select" selected disabled>Select</option>
<option value="op1">Option 1</option>
<option value="op2">Option 2</option>
</select>
JavaScript
function getval(sel) {
if (sel.value == "op1") {
alert("Option 1 Selected");
} else if (sel.value == "op2") {
alert("Option 2 Selected");
}
else
{
alert("EXCEPTION !");
}
}
Upvotes: 0
Reputation: 24001
you can use
$('select[name="roundType"]').prop('selectedIndex',0);
Upvotes: 0
Reputation: 4288
From my comment above, use onreset
event instead of onchange
:
$('#yourform').on('reset', function(){
// do something
});
Upvotes: 8