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);
});
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);
});
Share
Improve this question
edited May 13, 2015 at 12:32
Deep Kakkar
6,3054 gold badges43 silver badges80 bronze badges
asked May 13, 2015 at 12:19
MathiasMathias
2071 gold badge8 silver badges19 bronze badges
4
|
4 Answers
Reset to default 8From my comment above, use onreset
event instead of onchange
:
$('#yourform').on('reset', function(){
// do something
});
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');
});`
you can use
$('select[name="roundType"]').prop('selectedIndex',0);
DEMO HERE
This may help you, replace alert lines with your activity code.
JSFiddle
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 !");
}
}
reset
functionality. How you are doingreset
? – Guruprasad J Rao Commented May 13, 2015 at 12:20onreset
event, which is different thatonchange
. This is the cause. – kosmos Commented May 13, 2015 at 12:21