I want to be able to choose a input box and that the ones before it get triggered. I don't know how to do this with javascript.
for example if a user presses the first box only that box should be active but if the user presses the second box the first and second box should trigger.
the snippet bellow shows that I can active one box at a time but I want to be able to trigger the ones before it without having to manually do it once the second, third and last box had been choosen.
function color(campo) {
valor_campo = document.getElementById(campo).value;
if (valor_campo == 0) {
document.getElementById(campo).style.background = '#000';
document.getElementById(campo).style.color = '#000';
document.getElementById(campo).value = 1;
} else if (valor_campo == 1) {
document.getElementById(campo).style.background = '#fff';
document.getElementById(campo).style.color = '#fff';
document.getElementById(campo).value = 0;
}
}
function colors() {
if (document.getElementById('cc11_a').value == 0) {
document.getElementById('cc11_a').style.background = '#fff';
document.getElementById('cc11_a').style.color = '#fff';
document.getElementById('cc11_a').value = 0;
} else if (document.getElementById('cc11_a').value == 1) {
document.getElementById('cc11_a').style.background = '#000';
document.getElementById('cc11_a').style.color = '#000';
document.getElementById('cc11_a').value = 1;
}
if (document.getElementById('cc11_b').value == 0) {
document.getElementById('cc11_b').style.background = '#fff';
document.getElementById('cc11_b').style.color = '#fff';
document.getElementById('cc11_b').value = 0;
} else if (document.getElementById('cc11_b').value == 1) {
document.getElementById('cc11_b').style.background = '#000';
document.getElementById('cc11_b').style.color = '#000';
document.getElementById('cc11_b').value = 1;
}
if (document.getElementById('cc11_c').value == 0) {
document.getElementById('cc11_c').style.background = '#fff';
document.getElementById('cc11_c').style.color = '#fff';
document.getElementById('cc11_c').value = 0;
} else if (document.getElementById('cc11_c').value == 1) {
document.getElementById('cc11_c').style.background = '#000';
document.getElementById('cc11_c').style.color = '#000';
document.getElementById('cc11_c').value = 1;
}
if (document.getElementById('cc11_d').value == 0) {
document.getElementById('cc11_d').style.background = '#fff';
document.getElementById('cc11_d').style.color = '#fff';
document.getElementById('cc11_').value = 0;
} else if (document.getElementById('cc11_d').value == 1) {
document.getElementById('cc11_d').style.background = '#000';
document.getElementById('cc11_d').style.color = '#000';
document.getElementById('cc11_d').value = 1;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body onload="colors();">
<td colspan="3" style="text-align:left">
<input name="cc11_a" type="text" class="text" id="cc11_a" onclick="color(this.id);" style="width:0.3cm;" value="" />
<input name="cc11_b" type="text" class="text" id="cc11_b" onclick="color(this.id);" style="width:0.3cm;" value="" />
<input name="cc11_c" type="text" class="text" id="cc11_c" onclick="color(this.id);" style="width:0.3cm;" value="" />
<input name="cc11_d" type="text" class="text" id="cc11_d" onclick="color(this.id);" style="width:0.3cm;" value="" />
</td>