0

I'm writing a piece of code as an exercice in HTML that allows the user to tick boxes (the code below is simplified, from 8 checkboxes to 2) and the text totalPrice should show the price of the item selected.

<script type='text/javascript'>
function f(){
  if(document.form1.nano1Gb.checked == true)
    document.form1.totalPrice.value = document.form1.priceNano1Gb.value 
  if(document.form1.nano4Gb.checked == true)
    document.form1.totalPrice.value = document.form1.priceNano4Gb.value 
  if(document.form1.nano1Gb.checked == true && document.form1.nano4Gb.checked == true)
    document.form1.totalPrice.value = parseInt(document.form1.priceNano1Gb.value) + parseInt(document.form1.priceNano4Gb.value)
}
</script>
<body>
<form name='form1'>
<p>
<input type='checkbox' name='nano1Gb' onclick=f(); />
<input type='text' value='Nano 1GB'>
<input type='text' name='priceNano1Gb' value='90'</p>

<p>
<input type='checkbox' name='nano4Gb' onclick=f(); />
<input type='text' value='Nano 4 GBb'>
<input type='text' name='priceNano4Gb' value='155'</p>

<p><input type='text' name="totalPrice" placeholder="Total Price"></p>

This works with two elements, but with 8 elements, it seems highly inefficient for me to great hundreds of conditions checking every single box and totaling what else has been checked. How can I code this better?

0

2 Answers 2

2
  1. Give them a class.
  2. Use document.querySelectorAll(".yourclassname:checked")
  3. loop over result
  4. give total an ID and set the value

window.onload = function() {
  var checks = document.querySelectorAll(".chk");
  for (var i = 0; i < checks.length; i++) {
    checks[i].onclick = function() {
      total();
    }
  }
}

function getNext(next) {
  do next = next.nextSibling;
  while (next && next.nodeType !== 1);
  return next;
}

function total() {
  var total = 0;
  var checks = document.querySelectorAll(".chk:checked");
  for (var i = 0; i < checks.length; i++) {
    var nameField = getNext(checks[i]);
    var priceField = getNext(nameField);
    total += parseInt(priceField.value, 10); // or parsefloat(priceField.value) if decimal
  }
  document.querySelector("#total").value = total; // or total.toFixed(2) if decimal
}
<form name='form1'>
  <p>
    <input class="chk" type='checkbox' name='nano1Gb' />
    <input type='text' value='Nano 1GB'>
    <input type='text' name='priceNano1Gb' value='90' />
  </p>

  <p>
    <input class="chk" type='checkbox' name='nano4Gb' />
    <input type='text' value='Nano 4 GBb'>
    <input type='text' name='priceNano4Gb' value='155' />
  </p>

  <p>
    <input id="total" type='text' name="totalPrice" placeholder="Total Price">
  </p>
</form>

Sign up to request clarification or add additional context in comments.

Comments

1

Give the checkbox, value and total input elements a class.
Use querySelector/querySelectorAll to select these elements.
Using for loop traverse through each checkbox adding their value to the total if checked.
In this example I have used 5 checkboxes.

<script type='text/javascript'>
function f(){
      var val = document.querySelectorAll(".value");
      var check = document.querySelectorAll(".checkbox");
      var allSum = document.getElementByClassName(".total");
      var total = 0;
      for (var i=0; i<check.length; i++) {
        if(check[i].checked === true) {
            total += parseInt(val[i].value);
            allSum.value = total;          
          }
      }
}
</script>
</head>
<body>
<form name='form1'>
<p>
<input class="checkbox" type='checkbox' name='nano1Gb' onclick=f(); />
<input type='text' value='Nano 1GB'>
<input class="value" type='text' name='priceNano1Gb' value='90'</p>

<p>
<input class="checkbox" type='checkbox' name='nano2Gb' onclick=f(); />
<input type='text' value='Nano 2 GB'>
<input class="value" type='text' name='priceNano2Gb' value='115'</p>

<p>
<input class="checkbox" type='checkbox' name='nano3Gb' onclick=f(); />
<input type='text' value='Nano 3 GB'>
<input class="value" type='text' name='priceNano3Gb' value='130'</p>


<p>
<input class="checkbox" type='checkbox' name='nano4Gb' onclick=f(); />
<input type='text' value='Nano 4 GB'>
<input class="value" type='text' name='priceNano4Gb' value='155'</p>

<p>
<input class="checkbox" type='checkbox' name='nano5Gb' onclick=f(); />
<input type='text' value='Nano 5 GB'>
<input class="value" type='text' name='priceNano5Gb' value='170'</p>

<p><input class="total" type='text' name="totalPrice" placeholder="Total Price"></p>
</body>
</html>

3 Comments

This answer is incorrect since IDs need to be unique. Please see my answer for a solution using best practices.
Thank you for the correction, I have now edited ids to class.
But why bother? Nothing added to the existing answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.