Click here to Skip to main content
15,886,008 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hey, I was wondering how you go about displaying certain parts of an array in checkboxes. My example is that I have a select box, when a certain value is selected I want to be able to display certain parts of that array it has in common with another. Can create a quick JSFiddle if needed.

C#
var PRIVILEGE_PROFILES = [
    { PROFILE_ID: "ADickinson", COMPANY_CODE: "GW", PRIVILEGE_CODE: ("ALeave", "ALVwBlkLV", "ALAdmin", "DAAppAccess"), SECTOR: "01" }
];


That's the array that is displayed in my select box, when the PROFILE_ID is selected I want to display all the different PRIVILEGE_CODES it has into multiple check boxes inside a div. However the PRIVILEGE_CODE must match with a UNIQUE_CODE from this array then instead the privilege_code/unique_code being displayed in the checkboxes I need SEC_PRIVILEGES.Name to be displayed instead.
var SEC_Privileges = [
    { Unique_Code: "ALeave", Name: "Annual Leave Access" },
    { Unique_Code: "ALVwBlkLV", Name: "Annual Leave Blcok LeaveAccess" },
    { Unique_Code: "AlAdmin", Name: "Annual Leave Admin" },
];



This is the code for my actual select box, I've already done the methods to populate it with PROFILE_ID's

SQL
var profileselectHTML = "<select name='select_profile' id='select_profile' class='form_drop_down' data-role='none'>" +
   "<option>None</option>";
Posted
Updated 29-Jul-14 23:00pm
v2

1 solution

Hi,

Add a onchange event for the dropdown and added the following code and add a div in html as container for the checkboxes

JavaScript
$("#select_profile").change(function () {
        var checkboxes = "";
        $("#select_profile option:selected").each(function () {
            var value = $(this).text();

            $.each(PRIVILEGE_PROFILES, function (index, profile) {
                if (profile.PROFILE_ID === value) {
                    profile.PRIVILEGE_CODE.forEach(function (code) {
                        SEC_Privileges.forEach(function (arr) {
                            if (arr.Unique_Code === code) {
                                checkboxes += "<input type='checkbox'>" + arr.Name + "</input>";
                            }
                        });
                    });
                }
            });
        });
        $("#checkboxes").html(checkboxes);
    });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900