I am building a list of checkbox items in jQuery based on an array returned from a handler.
The containing element is the .listContainer
element below and then each dynamic element I want to add to this takes the form of the .listContainerItem
element. Each item will have checkbox value and label based on the array item creating it.
<div class="listContainer">
<div class="listContainerItem">
<input type="checkbox" value="1" />
<div class="listContainerItemLabel">Checkbox 1</div>
</div>
</div>
At the point of the function that has the array passed to it, what is the most efficient way of creating this? I have been trying to accomplish it as below, but quickly running into major performance issues.
function AddItemToListContainer(item) {
var currentItems = $j("#listContainerAvailable .listContainerItem");
if ($j.grep(currentItems, function (e) { return $j(e).find(":checkbox:first").val() == item.Id; }).length === 0) {
labelDiv = $j("<div />").addClass("listContainerItemLabel").html(item.Text);
itemToAdd = $j("<div />").addClass("listContainerItem").append("<input type=\"checkbox\" value=\"" + item.Id + "\" />").append(labelDiv);
currentItems.append(itemToAdd);
}
}
I've looked at .map
function, but not sure quite how to implement it. Can someone point me in the right direction?