M
M
Messi2017-09-15 16:30:04
JavaScript
Messi, 2017-09-15 16:30:04

Append element to dynamically created element?

There is such a task:

function addList(groups) {
          $.each(groups, function(i, val) {
            leftBlock
            .append(
                '<div class="statistic-item" data-attr="list-block">' + 
                '<div class="title-group" data-attr="list-title" group_id="'+ val.list_id +'">' + val.list_title + '</div>'
                + '</div>'
                );
          });
      }

I need to add a div.titl-group to a dynamically created div.statistic-item. The example shows that I did it in one append, just adding it to div.
But how is it more correct to do it through two append?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Skibin, 2017-09-15
@FitTech

function addList(groups) {
  $.each(groups, function(i, val) {
    var $static = $('<div/>');
    var $title = $('<div/>');

    $static.prop({
      'data-attr': 'list-block'
    });
    
    $title.prop({
      'data-attr': 'list-title',
      'group_id': val.list_id
    });
    $title.text(val.list_title);

    $static.append($title);
    leftBlock.append($static);
  });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question