Brian Var
Brian Var

Reputation: 6227

How to dynamically create a list of checkboxes from an array?

I've created a menu item list using an array by looping through each array item and assigning attributes accordingly.

Now I need to append a checkbox to each li item in the menu. What I did try is appending an input of type checkbox following by appending an input li tag. But this creates a menu list of input boxes.

Question:

How can you dynamically create a list of checkboxes from an array?

This is the current code I use in the script to create the menu:

    //Bind the country name list in sub menu
    var countries = ['United States', 'Canada', 'Argentina', 'Armenia'];
    var assetList = $('#assetNameMenu')
    $.each(countries, function(i)
    {
        var li = $('<li/>')
            .addClass('ui-menu-item')
            .attr('role', 'menuitem')
            .appendTo(assetList);
        var aaa = $('<a/>')
            .addClass('ui-all')
            .text(countries[i])
            .appendTo(li);
        var input = $('<input/>')
            .addClass('ui-all')
            .attr('role', 'checkbox')
            .appendTo(aaa);

    });



            <li>
                <a>All <span class="arrow">&#9660</span></a>
                <ul class="sub-menu" id="assetNameMenu">
                   @* li elements created in script *@
                </ul>
            </li>

Which creates the following with no checkboxes:

enter image description here

The aim is instead to dynamically create a list of checkbox items:

               <li>
                    <a>Default <span class="arrow">&#9660</span></a>
                    <ul class="sub-menu" id="assetNameMenu">
                        <li><a href="#" class="small" data-value="option1" tabindex="-1"><input type="checkbox" />&nbsp;Option 1</a></li>
                        <li><a href="#" class="small" data-value="option2" tabindex="-1"><input type="checkbox" />&nbsp;Option 2</a></li>
                        <li><a href="#" class="small" data-value="option3" tabindex="-1"><input type="checkbox" />&nbsp;Option 3</a></li>
                        <li><a href="#" class="small" data-value="option4" tabindex="-1"><input type="checkbox" />&nbsp;Option 4</a></li>
                        <li><a href="#" class="small" data-value="option5" tabindex="-1"><input type="checkbox" />&nbsp;Option 5</a></li>
                        <li><a href="#" class="small" data-value="option6" tabindex="-1"><input type="checkbox" />&nbsp;Option 6</a></li>
                    </ul>
                </li>

Which creates this the below:

enter image description here

Upvotes: 0

Views: 9388

Answers (2)

user5201742
user5201742

Reputation:

Just set the correct type for inputs and change order:

$.each(countries, function(i)
    {
        var li = $('<li/>')
            .addClass('ui-menu-item')
            .attr('role', 'menuitem')
            .appendTo(assetList);

        var input = $('<input/>')
            .addClass('ui-all')
            .attr('type', 'checkbox')
            .appendTo(li);

       var aaa = $('<a/>')
            .addClass('ui-all')
            .text(countries[i])
            .appendTo(li);

    });

enter image description here

Codepen: https://codepen.io/anon/pen/oLmQRp

UPDATE

For display:

.sub-menu {
 width: auto;
 [...]
}

.sub-menu li a { 
  text-align: left;
  [...]
}

Example: https://codepen.io/anon/pen/JKxwoO

Upvotes: 4

Mustapha Larhrouch
Mustapha Larhrouch

Reputation: 3393

you need to set the type to checkbox : .attr('type', 'checkbox')

$.each(countries, function(i)
    {
        var li = $('<li/>')
            .addClass('ui-menu-item')
            .attr('role', 'menuitem')
            .appendTo(assetList);
        var aaa = $('<a/>')
            .addClass('ui-all')
            .text(countries[i])
            .appendTo(li);
        var input = $('<input/>')
            .addClass('ui-all')
            .attr('type', 'checkbox')
            .appendTo(aaa);

    });

Upvotes: 0

Related Questions