I'm creating a simple tree structure which holds input field as child. Each child consist of one input field and two buttons. One button is for adding child for the input and another for removing the node. My code is shown in below snippet and it's not working as expected. The problem is that : I can't add more than one child only one child is getting added.
$("#main").on('click', '.add', function() {
var $td = $(this).parent();
var $td1 = $('<td>', {
html: '<input /> <button class="add"> Add </button> <button class="remove"> Remove </button>'
});
console.log($td.children('table').length);
if ($td.children('table').length)
$td.children('table').children('tr').append($td1);
else
$td.append($('<table>', {
html: $('<tr>', {
html: $td1
})
}))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="main" style="width:100%;text-align:center">
<tr>
<td>
<input type="text" />
<button class="add">
Add
</button>
<button class="remove">
Remove
</button>
</td>
</tr>
</table>