3
I have a form of 10 answers in which the person should put only 3 answers.
If she fills more than 3, the last input
typed must be erased.
Just follow my code:
It kind of works, but the problem is it doesn’t erase the last input
typed depending on the order I will fill in.
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("");
}
}
}
});