Generate dynamic li elements depending on how many checkboxes are checked

Viewed 88

I am trying to generate li elements for each checkbox that has been checked. It works when there is only one checked, but when I check 2 checkboxes, only the last checkbox and its hidden inputs (hidden inputs used to populate specific values inside the li element) are generated.

Here is the code I use now:

attachPanelListeners : function() {
            $('#sc-accordion').bind('click', function(e) {
                var items, 
                    target = $(e.target);

                if ( target.hasClass('submit-add-to-menu') ) {
                    var t = $(this).parent().parent(),
                        checkboxes = t.find('.sc-pagelist li input:checked');

                    // If no items are checked, bail.
                    if ( !checkboxes.length )
                        return false;

                    var structure;
                    $(checkboxes).each(function(){
                        var hidden = $(this).closest('li').find(':hidden');
                        structure = "<li class='menu-item menu-item-depth-0'>"+
                                        "<div class='menu-item-bar'>"+
                                            "<div class='menu-item-handle ui-sortable-handle'>"+
                                                "<span class='item-title'><span class='menu-item-title'>" + hidden[0].value + "</span></span>"+
                                                "<span class='item-controls'><span class='item-type'>Pagina</span><a href='#' class='item-edit'></a></span>"+
                                            "</div>"+
                                        "</div>"+
                                        "<div class='menu-item-settings sc-clearfix' style='display:none;'>"+
                                            "<p class='description description-wide'>"+
                                                "<label for='edit-menu-title'>Navigatielabel</label>"+
                                                "<input type='text' class='form-control' id='edit-menu-title' value='" + hidden[0].value + "'>"+
                                            "</p>"+
                                            "<fieldset class='field-move hide-if-no-js description description-wide'>"+
                                                "<span class='field-move-visual-label' aria-hidden='true'>Verplaatsen</span>"+
                                                "<button type='button' class='button-link menus-move menus-move-up' data-dir='up' style='display: none;'>Eén omhoog</button>"+
                                                "<button type='button' class='button-link menus-move menus-move-down' data-dir='down' style='display: inline;' aria-label='Verplaats één omlaag'>Eén omlaag</button>"+
                                                "<button type='button' class='button-link menus-move menus-move-left' data-dir='left' style='display: none;'></button>"+
                                                "<button type='button' class='button-link menus-move menus-move-right' data-dir='right' style='display: none;'></button>"+
                                                "<button type='button' class='button-link menus-move menus-move-top' data-dir='top' style='display: none;'>Naar boven</button>"+
                                            "</fieldset>"+
                                            "<div class='menu-item-actions description-wide submitbox'>"+
                                                "<a class='item-delete submitdelete deletion' id='' href='#'>Verwijderen</a>"+
                                            "</div>"+
                                            "<input type='hidden' value='"+ $(this).val() +"'>"+
                                            "<input type='hidden' value='"+ hidden[2].value +"'>"+
                                        "</div>"+
                                    "</li>";
                    });
                    $('#menu-to-edit').append(structure);
                    checkboxes.prop('checked', false);
                };
            })

        },

Cheers

2 Answers

You're assigning the string on each loop, rather than appending - meaning you only ever get the last one.

One option is to change...

structure = "<li class='menu-item menu-item-depth-0'>"+

To...

structure += "<li class='menu-item menu-item-depth-0'>"+

An alternative option is to append the string on each loop directly...

$(checkboxes).each(function(){
    var hidden = $(this).closest('li').find(':hidden');
    $('#menu-to-edit').append(
        "<li class='menu-item menu-item-depth-0'>"+
          "<div class='menu-item-bar'>"+
            ...
          "</div>"+
        "</li>");
});

I made a simple example below ( as you didn't share the HTML and a bunch of code you shared is not relevant to the problem itself )

Basically you need to append to the existing HTML. WHat you are doing is replacing it with a new li every time you check a checkbox. So it overrides the previous html.

I also removed the corresponding li when the checkbox is unchecked.

THere might be a more straight forward solution out there but this is the best i came up with for now :)

var chkBox = $(".checkbox")
$(chkBox).on('change', function(){

  var index = $(this).index()
  var menuItem = `<li class=${index}>${index} list item</li>`
    if ($(this).is(':checked') ) {
      $('.menu').html($('.menu').html() + menuItem)
    } else {
      $(`li.${index}`).remove()
    }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="menu">

</ul>
<div>
<input type="checkbox" class="checkbox"/>
<input type="checkbox" class="checkbox"/>
<input type="checkbox" class="checkbox"/>
<input type="checkbox" class="checkbox"/>
<input type="checkbox" class="checkbox"/>
<input type="checkbox" class="checkbox"/>
</div>

Related