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">▼</span></a>
<ul class="sub-menu" id="assetNameMenu">
@* li elements created in script *@
</ul>
</li>
Which creates the following with no checkboxes:
The aim is instead to dynamically create a list of checkbox items:
<li>
<a>Default <span class="arrow">▼</span></a>
<ul class="sub-menu" id="assetNameMenu">
<li><a href="#" class="small" data-value="option1" tabindex="-1"><input type="checkbox" /> Option 1</a></li>
<li><a href="#" class="small" data-value="option2" tabindex="-1"><input type="checkbox" /> Option 2</a></li>
<li><a href="#" class="small" data-value="option3" tabindex="-1"><input type="checkbox" /> Option 3</a></li>
<li><a href="#" class="small" data-value="option4" tabindex="-1"><input type="checkbox" /> Option 4</a></li>
<li><a href="#" class="small" data-value="option5" tabindex="-1"><input type="checkbox" /> Option 5</a></li>
<li><a href="#" class="small" data-value="option6" tabindex="-1"><input type="checkbox" /> Option 6</a></li>
</ul>
</li>
Which creates this the below:



.attr('role', 'checkbox')should be.attr('type', 'checkbox')<input>) cannot be nested within another interactive element (such as an<a>, or<button>).