dimanche 16 février 2020

Combinig output of 2 function in javascript

I am relatively new to Javascript (biopython background) and I am trying to add a simple feature to a website.

In essence, I need to accept user input for 4 types of menus (check boxes) and then multiply the sum of the values ($) of selected menus by the number of days and number of people (user inputs using text fields).

Below are HTML and Javascript parts of the code

function multiply() {
  var n_ppl = document.getElementsByName('people')[0].value;
  var n_days = document.getElementsByName('days')[0].value;
  return var out = n_ppl * n_days; 
}
  
function totalIt() {
  var n_ppl = document.getElementsByName('people')[0].value;
  var n_days = document.getElementsByName('days')[0].value;
  var input = document.getElementsByName("product");
  var total = 0;
  for (var i = 0; i < input.length; i++) {
    if (input[i].checked) {
      total += parseInt(input[i].value);
    }
  return var price = total
}

  // not sure how can I get - var final = total * out
  // after I get it I can finally do - document.getElementsByName("total")[0].value = "$" + final.toFixed(2);
<br> Breakfast <input name="product" value="12" type="checkbox" onclick="totalIt()" /> <br>
<br> First Meal <input name="product" value="20" type="checkbox" onclick="totalIt()" /> <br>
<br> Second Meal <input name="product" value="15" type="checkbox" onclick="totalIt()" /> <br>
<br> Craft Service <input name="product" value="10" type="checkbox" onclick="totalIt()" /> <br>
<br> Number of People <input type="text" name="people"> <br>
<br> Number of Days <input type="text" name="days"> <br>
<br> Total: <input value="$0.00" readonly="readonly" type="text" name="total" />

I apologize to all web developers for my pythonic style in advance.

I should also mention that both functions work perfectly fine separately but even without trying to multiply their outputs (i.e. even if their code is present next to each other), nothing works. I will be really grateful for your help!




Aucun commentaire:

Enregistrer un commentaire