Saturday, 21 October 2017

MVC - JavaScript - How to clear Select List

Watch this example on YouTube:


HTML:

<select id="Product">
    <option>Product1</option>
    <option>Product2</option>
    <option>Product3</option>
    <option>Product4</option>
    <option>Product5</option>
</select>
<input type="submit" value="clear select list" id="btnClear" />

JAVASCRIPT:

@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnClear").click(function (e) {
                var SelectOption = document.getElementById("Product");
                for (var i = SelectOption.options.length - 1; i >= 0; i--) {
                    SelectOption.remove(i);
                }
            });
        });
    </script>
}

No comments:

Post a Comment