Good morning, the question is this. I have a small Form that is not more than a Form Generator, each time I select a component and I believe it is shown in another DIV, this form grants a random own ID, but when I want to create another element, this element is assigned with the same ID as the previous element.
My question is, could you help me.
1) As I do so that when creating another element, it is added with another ID.
I tried the Jquery .load function, but for some reason every time you refresh my form, it loses all its Javascript functionality that allows you to create the elements, validate and deliver the ID.
Any way to solve it? Thanks in advance.
Here the Code.
The Function of the Button when filling in the Form:
/*botón de añadir*/
$("#btn_add").on("click",function(){
var f = $("#miCampo");
f.append("<div class='field'><label>"+$("#nom_camp").val()+": <input placeholder='"+$("#plch").val()+"' type='"+$('#select_opc').val()+"' name='"+(galpha+"_"+$("#nom_camp").val())+"' id='"+(galpha+"_"+$("#nom_camp").val())+"' class='"+(galpha+"_"+$("#nom_camp").val())+"'></label></div>");
plch.prop('disabled', true);
nc.prop('disabled', true);
nc.val("");
plch.val("");
$("#nom_camp").val("");
$("#id_id").text("");
$("#id_name").text("");
$("#id_class").text("");
$("#form_refresh").load(location.href + " #form"
});
})
The Function that generates the random ID
/*función que genera una letra y numero de forma aleatoria*/
function generarAlphanumerico(r)
{
var arreglo = new Array();
var r = Math.floor(Math.random()*100) +1;
for(var i=65; i<=90; i++)
{arreglo[i] = String.fromCharCode(i);}
return arreglo[Math.floor(Math.random() * (arreglo.length-65)+65)]+''+r;
}
Form:
<!--Área de trabajo -->
<div class="ui stripe community vertical segment">
<div class="ui two column center divided very relaxed stackable grid container">
<div class="row">
<div class="column">
<!-- área donde se visualizará el formulario -->
<form class="ui form">
<h4>Mi formulario</h4>
<div id="miCampo"></div>
<input type="button" value="Enviar" class="ui button" id="btn_submit_generado">
</form>
</div>
<div class="column" id="form_refresh">
<form action="" class="ui form" id="form">
<h4 class="ui dividing header">
Seleccionar
</h4>
<div class="field">
<label>Tipo de campo:</label>
<select class="ui fluid dropdown" id="select_opc">
<option value="0">Seleccionar</option>
<option value="label">Etiqueta</option>
<option value="text">Caja de texto</option>
<option value="textarea">Cuadro de texto</option>
<option value="checkbox">Casilla de verificación</option>
<option value="radio">Botón de radio</option>
<option value="select">Desplegable</option>
<option value="date">Fecha</option>
<option value="number">Número</option>
</select>
</div>
<div class="field">
<label>Nombre del campo</label>
<input type="text" id="nom_camp">
</div>
<div class="field">
<label>id: <span id="id_id"></span></label>
</div>
<div class="field">
<label>Name: <span id="id_name"></span></label>
</div>
<div class="field">
<label>Class: <span id="id_class"></span></label>
</div>
<div class="field">
<label>Placeholder:</label>
<input type="text" id="plch">
</div>
<div class="ui green vertical animated button" id="btn_add" style="float:right;">
<div class="hidden content">Add</div>
<div class="visible content">
<i class="add icon"></i>
</div>
</div>
</form>
</div>
</div>
</div>
</div>