0
I’m creating this Javascript function, it creates a form and adds in the body of text in another div, only when I click the button it deletes the content that was already written in the input.
<button onclick="addPergunta(); return false" class="btn btn-warning btnPrincipal" name="2">ABERTA <i class="glyphicon glyphicon-plus-sign"></i> </button>
<script>
var conti = 0;
function addPergunta() {
conti++;
var htmlNovo = "";
htmlNovo +=
'<div class="main-login main-center">' +
'<form class="form-horizontal" method="post" action="#">' +
'<div class="form-group">' +
'<label for="name" class="cols-sm-2 control-label">Digite sua pergunta.</label>' +
'<div class="cols-sm-10">' +
'<div class="input-group">' +
'<span class="input-group-addon"><i class="fa fa-user fa" aria-hidden="true"></i></span>' +
'<input type="text" class="form-control" name="pergunt' + conti + ' " id="name" placeholder="Qual seu nome?" />' +
'</div></div></div></form></div></div></div></div></div>' + '';
document.getElementById("multiplaescolha").innerHTML += htmlNovo;
return false;
}
</script>
try to use
appendChild
instead of adding to innerHTML: https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild– Ricardo Pontual