Mathias
Mathias

Reputation: 207

html form reset not doing triggering select onchange

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

Answers (4)

Nifal Munzir
Nifal Munzir

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

divy3993
divy3993

Reputation: 5810

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 !");
    }
}

Upvotes: 0

Mohamed-Yousef
Mohamed-Yousef

Reputation: 24001

you can use

$('select[name="roundType"]').prop('selectedIndex',0);

DEMO HERE

Upvotes: 0

kosmos
kosmos

Reputation: 4288

From my comment above, use onreset event instead of onchange:

$('#yourform').on('reset', function(){
    // do something
});

Upvotes: 8

Related Questions