locked
How to prevent select onchange from firing when knockout populates the select list RRS feed

  • Question

  • User-1771888428 posted

    Hello,

    when knockout populates the select list the onchange event that I attached to the select list fires repeatedly and my code: $("#ddlCategories").val($("#hdnCat").val()); that sets the selectedvalue is not working, when I remove the onchange event from ddlCategories code works fine, So my question is how to prevent the change event from firing when programmaticaly populating the select list?

    Wednesday, October 29, 2014 1:39 AM

Answers

  • User-271186128 posted

    Hi mz1378,

    According to your description, I'm not very sure about your question. If you want to get the selected value from the select list, you could reference to the following code.

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
        <script type="text/javascript">
            $(document).ready(function () {           
                $("#testSelect").append($("<option>").attr("value", "1").attr("selected", "selected").text("---Select Field Name---"));
                $("#btnAdd").click(function () {
                    //Add new option.
                    $("#testSelect").append($("<option>").text($("#txtNew").val()));
                });
                $("#testSelect").change(function () {
                    //Show selected value.
                    alert("Text:" + $("#testSelect option:selected").text() + "    Value:" + $("#testSelect option:selected").val());
                });
            });
        </script>
    
        <input id="txtNew" type="text" />
        <input id="btnAdd" type="button" value="AddValue" /><br />
        <select id="testSelect">
        </select>

    >>how to prevent the change event from firing when programmaticaly populating the select list?

    For this issue, the onchange event will be trigger when the selected value change. So, when we populate the select list, the onchange event not fire.

    If you have any other question about my reply, please let me know freely.

    Best Regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 30, 2014 1:58 AM