I have the following code that has 3 parts - 2 sets of checkboxes and some auto-displaying text dependant on the 2nd checkboxes.
function addTotals() { var input = document.getElementsByName("score"); var total = 0; for (var i = 0; i < input.length; i++) { if (input[i].checked) { total += parseFloat(input[i].value); } } document.getElementsByName("total")[0].value = "" + total.toFixed(0); } function displayText(checkBoxElm,textBoxId) { let textBox = document.getElementById(textBoxId); if (checkBoxElm.checked){ textBox.style.display = "block"; } else { textBox.style.display = "none"; } }
<table id="scorecard" border="1"> <tr> <td><input name="score" value="40" type="checkbox" onclick="addTotals()" />Impossible</td> <td><input name="score" value="30" type="checkbox" onclick="addTotals()" />Hard</td> <td><input name="score" value="20" type="checkbox" onclick="addTotals()" />Medium</td> <td><input name="score" value="10" type="checkbox" onclick="addTotals()" />Easy</td> <td bgcolor="red" rowspan="2" style="width:90px;text-align:center" ><input style="width:60px;text-align:center" value="0" readonly="readonly" type="text" name="total" /></td> </tr> </table> <table id="report" border="1"> <tr> <td><input type="checkbox" id="box1" onclick="displayText(this,'textBox1')"><label for="box1">Option 1</label></td> <td><input type="checkbox" id="box2" onclick="displayText(this,'textBox2')"><label for="box2">Option 2</label></td> <td><input type="checkbox" id="box3" onclick="displayText(this,'textBox3')"><label for="box3">Option 3</label></td> <td><input type="checkbox" id="box4" onclick="displayText(this,'textBox4')"><label for="box4">Option 4</label></td> <td><input type="checkbox" id="box5" onclick="displayText(this,'textBox5')"><label for="box5">Option 5</label></td> </tr> </table> <p id="main" style="display:block" contenteditable="true"> <span id="textBox1" style="display:none">Pears.</span> <span id="textBox2" style="display:none">Apples.</span> <span id="textBox3" style="display:none">Oranges.</span> <span id="textBox4" style="display:none">Lemons.</span> <span id="textBox5" style="display:none">Excel.</span> <p>
** edit: (using only javascript and html on 1 page (no db's, externals, etc.)). I was having issues with the onclick cascading effect of colour changing and checkbox ticking, especially on the reversals, and my code got all messed up :(
Based on the total value which you already have, you can decide the color by simple if-else condition, and by traveling through each checkbox from min value to max you can go on deciding if checkbox needs to be checked or not
function addTotals() {
var input = document.getElementsByName("score");
var total = 0;
for (var i = 0; i < input.length; i++) {
if (input[i].checked) {
total += parseFloat(input[i].value);
}
}
document.getElementsByName("total")[0].value = "" + total.toFixed(0);
for(travel through each checkbox from min value to max)
{
if(total >= checkbox value)
{
total= total - checkox value
checkbox.checked=true
}
else{
checkbox.checked=false
}
}
if(condition1)
{
target.bg = "color1"
}
else if(condition2)
{
target.bg = "color2"
}
so on for rest
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.