I have a 10-question form in which the person should put only 3 answers.
If it fills more than 3, the last input
entered should be deleted.
Here is my code:
It even works, but the problem is that it does not delete the last input
entered depending on the order that I will fill.
HTML
<input type="text" name="resposta1">
<input type="text" name="resposta2">
<input type="text" name="resposta3">
<input type="text" name="resposta4">
<input type="text" name="resposta5">
<input type="text" name="resposta6">
<input type="text" name="resposta7">
<input type="text" name="resposta8">
<input type="text" name="resposta9">
<input type="text" name="resposta10">
<input type="button" id="btn-bloco" value="Aperte">
Javascript
var totalDeRespostas = 10;
$("#btn-bloco").on("click",function(){
var totalDeRespostasRespondidas = 0;
for(var i = 0; i< totalDeRespostas;i++){
if($("[name='resposta"+(i+1)+"']").val() != ""){
totalDeRespostasRespondidas++;
if(totalDeRespostasRespondidas > 3){
var aux = $("[name='resposta"+(i+1)+"']").val();
alert(aux);
$("[name='resposta"+(i+1)+"']").val("");
}
}
}
});