Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically creating elements using jQuery

Tags:

jquery

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?

like image 490
Stewart Alan Avatar asked Mar 04 '13 12:03

Stewart Alan


People also ask

How dynamically add HTML element using jQuery?

You can use jQuery DOM manipulation methods like append(), appendTo() or html() to add new HTML elements like div, paragraph, headings, image into DOM without reloading the page again.

How can you add new elements dynamically?

Creation of new element: New elements can be created in JS by using the createElement() method. Syntax: document. createElement("<tagName>"); // Where <tagName> can be any HTML // tagName like div, ul, button, etc. // newDiv element has been created For Eg: let newDiv = document.


2 Answers

I would start with something like this:

var $container = $j('#listContainerAvailable');
var checkboxes = {};

function AddItemToListContainer(item) {
    if (checkboxes[item.Id]) return false;

    checkboxes[item.Id] = true;

    var $item = $j('<div />', {
        'class': 'listContainerItem',
    }).appendTo($container);

    $j('<input />', {
        'type': 'checkbox',
        'value': item.Id
    }).appendTo($item);

    $j('<div />',  {
        'class': 'listContainerItemLabel',
        'text': item.Text
    }).appendTo($item);
}

As long as none of these elements exist when you create the page, you can add them to a hash table instead of searching through the DOM. I think you'd also benefit from a JS templating engine like mustache.js

like image 175
Blender Avatar answered Nov 15 '22 10:11

Blender


Are you wanting to have these elements appear one at a time? or just be dynamically created from an array on page load?

For the last:

html

    <div class="listContainer">
       <div class="listContainerItem"></div>
     </div>  

and jquery

var array = ['1', '2', '3', '4', '5'];

 $.each(array, function (index, value) {
    $(".listContainerItem").append('<input type="checkbox" value="' + value + '" /> <div         class="listContainerItemLabel">Checkbox ' + value + '</div>');
 });

http://jsfiddle.net/jeremythuff/HEKjk/

And some .click events can give you the first effect

like image 34
Jeremythuff Avatar answered Nov 15 '22 09:11

Jeremythuff