Trigger DropDownList OnChange event from second time selection in jQuery

Last Reply one year ago By dharmendr

Posted one year ago

I have a dropdown which has select by default in it.

If now user selects any other value other than select for the first time my change event should not be triggeredand my change event should work from second time.

Is there any way to do this.

<select id="ddl1" class="width94P selectHeight" onchange="onPhConsentFormSelect()">
<option value="-1"> Select </option>
<option value="0" title="2">Samsung</option>
<option value="1" title="3">Oppo</option>
<option value="2" title="4">Real Me</option>
</select>

what i tried

$("#ddl1").on('change', function () {
    if ($("#ddl1").val() != "-1") {
        $('#txtmode').val('');
        $('#txtcolor').val('');
        $('#txtcustomization').val('');
        $('#txtexpecteddate).val('');
    }
});

currently my trigger change event which i had written is working from the first time of drop down selection change only but it should work from second time.

Posted one year ago Modified on one year ago

Hi kavithav,

Refer below code.

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div>
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.3.js"></script>
        <script type="text/javascript">
            $(function () {
                var count = 0;
                $("#ddl1").on('change', function () {
                    if (count > 0) {
                        if ($("#ddl1").val() != "-1") {
                            alert($("#ddl1").val());
                        }
                    } else {
                        count++;
                    }
                });
            });
        </script>
        <select id="ddl1">
            <option value="-1">Select </option>
            <option value="0" title="2">Samsung</option>
            <option value="1" title="3">Oppo</option>
            <option value="2" title="4">Real Me</option>
        </select>
    </div>
</body>
</html>

Demo