I have a problem when cloning a complete table in JavaScript: pressing the button that allows me to clone the table deactivates me the buttons that allow me to clone text fields inside it; and vice versa: if I press the buttons inside the table that clone me the text fields, I disable the buttons that allow me to clone the table.
The following image shows how the tables I want to implement are:
And this is the code I use:
$(document).ready(function() {
tabla = $('#tabla');
$('#agregartabla').live('click', function() {
$(this).parents(".cl1").clone().appendTo("div#cl0").find(':text').val('');
});
$(".eliminartabla").live('click', function() {
$(this).closest(".cl1").remove();
});
$('#btnAdd').click(function() {
var num = $('.clonedInput').length;
var newNum = new Number(num + 1);
var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);
newElem.children(':first').attr('id', 'name' + newNum)
.attr('name', 'name' + newNum).find(':text').val('');
$('#input' + num).after(newElem);
$('#btnDel').attr('enabled', '');
if (newNum == 10)
$('#btnAdd').attr('disabled', 'disabled');
});
$('#btnDel').click(function() {
var num = $('.clonedInput').length;
$('#input' + num).remove();
$('#btnAdd').attr('enabled', '');
if (num - 1 == 0)
$('#btnDel').attr('disabled', 'disabled');
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<div id="cl0">
<div id="cl1" class="cl1">
<input type="button" value="Agregar" id="agregartabla">
<input type="button" value="Eliminar" class="eliminartabla">
<table width="70%" border="2" id="tabla">
<tr>
<td>
<ol>
<li>ARTICULO<input type="text" name="name2" id="name2" /></li>
</ol>
</td>
<td>
<ol>
<div id="input1" class="clonedInput">
<li>MARCA<input type="text" name="name3" id="name3" /></li>
</div>
</ol>
<input type="button" id="btnAdd" value="AGREGAR MARCA" />
<input type="button" id="btnDel" value="BORRAR MARCA" />
</td>
</tr><br>
</table><br>
</div>
</div>