JQuery dynamic elements like TR AND TD, add to HTML TABLE

s it possible to create dynamic TR and TD elements in an HTML table? Something similar but better than this:

jQuery(document).ready(function() {
  $('button[name="new-title"]').on('click', function(){
    var table = $('table[name="errortitle"]');
    var tr = $('<tr />');
    var td = $('<td />');
    var input = $('<input />').attr({'class' : 'form-control'})
    var button = $('<button />').attr({'class' : 'btn'});
    var checkbox = input.clone().attr({'type' : 'checkbox', 'name' : 'check'});

    var tdId = td.clone().html('-');
    var tdTitle = td.clone().append(input.addClass('formInput').attr({'type': 'text'}));
    var tdCheckBox = td.clone().append(checkbox);
    var tdAction = td.clone().html(button.addClass('btn-danger').html('UsuĊ„'));

    tr.append(tdCheckBox);
    tr.append(tdId);
    tr.append(tdTitle);
    tr.append(tdAction);

    table.append(tr);
  });
});

Is it possible to make this code nicer or more efficient?

Answers


You can use something like this:

var input = $('<input />').attr({'class' : 'form-control'})
var button = $('<button />').attr({'class' : 'btn'});
var checkbox = input.clone().attr({'type' : 'checkbox', 'name' : 'check'});


$("#tid").find('tbody').append($('<tr>').append($('<td>').append(input)).append($('<td>').append(button)).append($('<td>').append(checkbox)));

working fiddle


Need Your Help

unity 5, how to open saved scene?

unity3d unity5

when I start a new unity 5 2D game. By default, it has scene, game and asset Store tabs. I saved the scene and named it to movementStaging, then closed the scene tab. Now i cannot open it again. I ...