3
Guys, I’m a beginner and I’m having a hard time running this code I used from another topic right here (Add and Remove Fields with Javascript).
Here it did not run, but on my localhost it even clones the requested snippet, but not only shows select.
I confirm that the select is also cloned by "Inspect element" browser.
Please point out where I’m making the mistake, or if I’m missing something.
Grateful, from now on.
$(function() {
var divContent = $('#materialInst');
var botaoAdicionar = $('a[data-id="1"]');
var i = 1;
//Ao clicar em adicionar ele cria uma linha com novos campos
$(botaoAdicionar).click(function() {
$('<div class="conteudoIndividual"><div><input type="number" name="qitem0' + i + '" id="qitem0"><label for="qitem0">Quantidade</label></div><div><select id="item0" name="item0' + i + '"><option value="" disabled selected>Material</option><option value="1">item1</option><option value="2">item2</option><option value="3">item3</option></select></div><div><input disabled type="number" name="vitem0' + i + '" id="item0"><label for="vitem0">Valor unitário R$</label></div><div><input disabled type="number" name="vtitem0' + i + '" id="vtitem0"><label for="vtitem0">Valor total R$</label></div></div>').appendTo(divContent);
$('#removehidden').remove();
i++;
$('<input type="hidden" name="quantidadeCampos" value="' + i + '" id="removehidden">').appendTo(divContent);
});
//Cliquando em remover a linha é eliminada
$('#materialInst').on('click', '.linkRemover', function() {
$(this).parents('.conteudoIndividual').remove();
i--;
});
});
<div id="materialInst">
<div>
<div>
<input type="number" name="qitem0" id="qitem0">
<label for="qitem0">Quantidade</label>
</div>
<div>
<select id="item0" name="item0">
<option value="" disabled selected>Material</option>
<option value="1">item1</option>
<option value="2">item2</option>
<option value="3">item3</option>
</select>
</div>
<div>
<input disabled type="number" name="vitem0" id="item0">
<label for="vitem0">Valor unitário R$</label>
</div>
<div>
<input disabled type="number" name="vtitem0" id="vtitem0">
<label for="vtitem0">Valor total R$</label>
</div>
</div>
</div>
<a href="#" id="adicionar" data-id="1">Clonar</a>
Solution: It was the framework(materialize). I pulled out all the classes and it worked.
Actually not @rafael. This was my fault, it was my first publication here, so I apologize. My question is about select, because on my host site it gets Hidden after cloning.
– Sander Roosevelt
It was the framework(materialize). I removed all classes and it worked. Thank you!
– Sander Roosevelt