Storing variable from calculated inputs – javascript

Welcome ! I have a question. Is the any possibility to store calculated data from three inputs for further math calculations? My clue is to store this dowform.value after calculations in new variable so i could for example divide this calculated value or multiply it and get new value. Code below for better understanding:

Javascript:

function dryOperatingWeight() {
  const bew = document.getElementById("bew").value;
  const crew = document.getElementById("crew").value;
  const pantryDiv = document.getElementById("pantry").value;
  let arr = document.getElementsByName("dow");
  let tot = 0;
  for (var i = 0; i < arr.length; i++) {
    if (parseInt(arr[i].value)) tot += parseInt(arr[i].value);
  }
  dowform.value = tot;
}

HTML:

<div class="form-group">
              <label for="bew">Basic Empty Weight</label>
              <input class="form-control"  name="dow" type="text" id="bew" value="" onkeyup = "dryOperatingWeight()"/>
            
          </div>
            <div class="form-group">
              <label id="crew"> Crew Weight</label>
              <input
                type="number"
                name="dow"
                class="form-control"
                id="crew"
                value=""
                placeholder="crew weight"
                onkeyup = "dryOperatingWeight()"
              />

              <div class="form-group">
                <label id="pantry">Pantry Weight</label>
                <input
                  type="number"
                  name="dow"
                  class="form-control"
                  id="pantry"
                  placeholder="Enter pantry code"
                  onkeyup = "dryOperatingWeight()"
                />
          </div>

          <div class="form-group">
            <label id="pantry">Water Weight</label>
            <input
              type="number"
              name="dow"
              class="form-control"
              id="water"
              placeholder="Enter water weight"
              onkeyup = "dryOperatingWeight()"
            />
      </div>
          <div id="dow">
            <div class="form-group">
              <label for="dryoperating weight">Dry Operating Weight</label>
              <input class="form-control" type="number" id="dowform" readonly/>
            </div>
          </div>

51 thoughts on “Storing variable from calculated inputs – javascript”

  1. You can declare your variable outside of the function and just reuse it and modify it as needed.

    let dow;
        
        function dryOperatingWeight() {
          const bew = document.getElementById("bew").value;
          const crew = document.getElementById("crew").value;
          const pantryDiv = document.getElementById("pantry").value;
          let arr = document.getElementsByName("dow");
          dow = 0;
          for (var i = 0; i < arr.length; i++) {
            if (parseInt(arr[i].value)) dow += parseInt(arr[i].value);
          }
          dowform.value = dow;
        }
    
    Reply

Leave a Comment