I start by telling you that I have no experience with jquery and at this moment I just want to give a space each time I add an input but I can not find the form, I have put the label here and it does not work:
$(container).append('<input type=text required="required" name ="fields[]" class="input" id=tb' + iCnt + ' ' +
'placeholder="Producto ' + iCnt + '" /> ');
I leave my full code:
<script>
$(document).ready(function() {
var iCnt = 0;
// Crear un elemento div añadiendo estilos CSS
var container = $(document.createElement('div')).css({
padding: '5px', margin: '20px', width: '170px', border: '1px dashed',
borderTopColor: 'blue', borderBottomColor: 'blue',
borderLeftColor: 'blue', borderRightColor: 'blue'
});
$('#btAdd').click(function() {
if (iCnt <= 10) {
iCnt = iCnt + 1;
// Añadir caja de texto.
$(container).append('<input type=text required="required" name ="fields[]" class="input" id=tb' + iCnt + ' ' +
'placeholder="Producto ' + iCnt + '" /> ');
if (iCnt == 1) {
var divSubmit = $(document.createElement('div'));
$(divSubmit).append('<input type=submit name="btn-add" onclick="GetTextValue()"' +
'id=btSubmit value=Enviar />');
}
$('#main').after(container, divSubmit);
}
else { //se establece un limite para añadir elementos, 20 es el limite
$(container).append('<label>Limite Alcanzado</label>');
$('#btAdd').attr('class', 'bt-disable');
$('#btAdd').attr('disabled', 'disabled');
}
});
$('#btRemove').click(function() { // Elimina un elemento por click
if (iCnt != 0) { $('#tb' + iCnt).remove(); iCnt = iCnt - 1; }
if (iCnt == 0) { $(container).empty();
$(container).remove();
$('#btSubmit').remove();
$('#btAdd').removeAttr('disabled');
$('#btAdd').attr('class', 'bt')
}
});
$('#btRemoveAll').click(function() { // Elimina todos los elementos del contenedor
$(container).empty();
$(container).remove();
$('#btSubmit').remove(); iCnt = 0;
$('#btAdd').removeAttr('disabled');
$('#btAdd').attr('class', 'bt');
});
});
// Obtiene los valores de los textbox al dar click en el boton "Enviar"
var divValue, values = '';
function GetTextValue() {
$(divValue).empty();
$(divValue).remove(); values = '';
$('.input').each(function() {
divValue = $(document.createElement('div')).css({
padding:'5px', width:'200px'
});
values += this.value + '<br />'
});
$(divValue).append('<p><b>Tus valores añadidos</b></p>' + values);
$('body').append(divValue);
}
</script>