Search code examples
javascriptjquerybootstrap-multiselect

Textbox value to check multiselect parameter based on values JS


Trying to assign ticks to a checkbox and have options selected if the value matches the values in the textbox. This a multi-select parameter.

E.g. if the textbox contains 1,2 when the page refreshes I want to ensure Cheese and Tomatoes is selected. If the textbox contains 1,5,6 then I want to ensure Cheese, Pepperoni and Onions are selected. If the textbox contains 1,2,3,4,5,6 then I want to ensure all checkboxes are selected.

Trying to write some javascript to do this. I tried to use local storage but can't get it working. See code example: https://www.codeply.com/go/rupzwTzBMY

ASPX:

<input type="text" runat="server" id="txt1" visible="true" value="" /> 

<div class="container">
    <select id="basic" multiple="multiple">
        <option value="1">Cheese</option>
        <option value="2">Tomatoes</option>
        <option value="3">Mozzarella</option>
        <option value="4">Mushrooms</option>
        <option value="5">Pepperoni</option>
        <option value="6">Onions</option>
    </select>
</div>

Currently when the page refreshes, even if the textbox has values assigned - the checkboxes clear and nothing is selected. I am trying to ensure when users select items from the multi-parameter when the page refreshes those values are not gone and still remain on the page.

Javascript functionality that works so far. This puts values in a textbox when you select items from the drop-down. However, when the page refreshes the text box keeps these selected values but the multi-select parameter doesn't:

 $('#basic').multiselect({
    onChange: function () {
        var selectedOptions = $('#basic').val();
        document.getElementById("txt1").value = selectedOptions;

    },


});

Solution

  • Firstly, you must reload the select once after populating default values as below

    $('#basic').multiselect('refresh')

    Secondly, try to use onInitialized method as described here

    Finally, You're trying to assign back values from TextBox to Dropdown as below, where you're trying to assign the value as such with a comma 1,2 which actually doesn't exist as a value in dropdown.

    External Fiddle

    $('#basic').children("option[value=" +
    document.getElementById("txt1").value + "]").prop("selected", true);
    

    Split the values as 1 and 2 and then assign and it works.

    $(document).ready(function () {
    
        document.getElementById("txt1").value = "1,2,3"
    
        $('#basic').multiselect({
                includeSelectAllOption: true,
                numberDisplayed: 5,
                onInitialized: function(select, container) {
                    console.log("Init");
                    selectListValues();
                }
            });
            
            $("#basic").multiselect('refresh');
    });
    
    function selectListValues()
    {
        var txtValue = document.getElementById("txt1").value;
        var selectedValues = txtValue.split(",");
        
        for(var i = 0; i < selectedValues.length; i++)
        {
            var val = selectedValues[i];
            
            if (val == null || val == "") continue;
            
          $('#basic').children("option[value=" + val + "]").prop("selected", "selected");
        }
    }
    <html>
    <head>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.15/css/bootstrap-multiselect.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
    </head>
    <body>
    
    
     <input type="text" runat="server" id="txt1" visible="true" value="" onchange="selectListValues()" /> 
    
    <div class="container">
        <select id="basic" multiple="multiple">
            <option value="1">Cheese</option>
            <option value="2">Tomatoes</option>
            <option value="3">Mozzarella</option>
            <option value="4">Mushrooms</option>
            <option value="5">Pepperoni</option>
            <option value="6">Onions</option>
        </select>
    </div>
    
    </body>
    </html>