Home > Software engineering >  Javascript cant get my value and calculate in html
Javascript cant get my value and calculate in html

Time:06-13

Im trying to do simple calculation for the fee ,but its doesn't work ,there's no error in the code. Did I miss something in the script ?

    <script type="text/javascript">
        var bwm = 7.9;
        var bswk = 14;
        var bsbh = 15;
        var wm = 2;
        var swk = 11;
        var sbh = 12;
        var kilo, overkilo, f;
        var s = document.getElementById('place');
        var place = s.options[s.selectedIndex].value;
        var k = document.getElementById('kilo').value;
        var tot;

        function quote() {
            f = document.getElementById('theform');
            f.reset();

            document.getElementById('calc').onclick = function() {
                if (place == 'swk') {
                    (k * swk)   bswk = tot;

                } else if (place == 'sbh') {
                    (k * sbh)   bsbh = tot;

                } else {
                    (k * wm)   bwm = tot;

                }
                document.getElementById('tot').value = 'RM  '   parseFloat;
            }

        }
    </script>

    <form id="theform" action="#">
        <div>
            <label for="place">Choose Destination :</label>
            <select id="place" onChange="quote()">
                <option value="swk">Sarawak</option>
                <option value="sbh">Sabah</option>
                <option value="wm">WestMalaysia</option>
            </select>
        </div>
        <div>
            <label for="kilo">Amount of KG :</label>
            <input id="kilo" type="text">
        </div>
        <div>
            <label>Total :</label>
            <input id="tot" type="text" readonly="readonly">
        </div>
        <div>
            <label></label>
            <input id="calc" type="button" value="calculate">
            <input id="r" type="reset" value="clear">
        </div>
    </form>

The clear works fine ,but the calculate button won't work even i have input the KG and select a option to calculate .

CodePudding user response:

You need to move the definition of the click handler outside of the change handler, unless the click handler would be defined only when an option changes and also it would be defined on every option change which is unnecessary.

Grab all the required values inside the click handler otherwise you would not have the updated values.

And you also need to set the selected index after resetting the form otherwise the change of option would not be visible.

const 
  bwm = 7.9,
  bswk = 14,
  bsbh = 15,
  wm = 2,
  swk = 11,
  sbh = 12;

function quote(e) {
  const selIndex = e.target.selectedIndex;
  document.getElementById("theform").reset();
  document.getElementById("place").selectedIndex = selIndex;
}

document.getElementById("calc").onclick = function () {
  const select = document.getElementById("place");
  const place = select.options[select.selectedIndex].value;
  const k = document.getElementById("kilo").value;
  if (!k) {
    return;
  }
  let tot;
  if (place === "swk") {
    tot = k * swk   bswk;
  } else if (place === "sbh") {
    tot = k * sbh   bsbh;
  } else {
    tot = k * wm   bwm;
  }
  document.getElementById("tot").value = "RM  "   tot;
};
<form id="theform" action="#">
  <div>
    <label for="place">Choose Destination :</label>
    <select id="place" onChange="quote(event)">
      <option value="swk">Sarawak</option>
      <option value="sbh">Sabah</option>
      <option value="wm">WestMalaysia</option>
    </select>
  </div>
  <div>
    <label for="kilo">Amount of KG :</label>
    <input id="kilo" type="text">
  </div>
  <div>
    <label>Total :</label>
    <input id="tot" type="text" readonly="readonly">
  </div>
  <div>
    <label></label>
    <input id="calc" type="button" value="calculate">
    <input id="r" type="reset" value="clear">
  </div>
</form>

Instead of resetting the form you could also update the calculated value every time the option changes.

const 
  bwm = 7.9,
  bswk = 14,
  bsbh = 15,
  wm = 2,
  swk = 11,
  sbh = 12;

document.getElementById("calc").onclick = handleClick;

function handleClick() {
  const select = document.getElementById("place");
  const place = select.options[select.selectedIndex].value;
  const k = document.getElementById("kilo").value;
  if (!k) {
    return;
  }
  let tot;
  if (place === "swk") {
    tot = k * 10   10;
  } else if (place === "sbh") {
    tot = k * sbh   bsbh;
  } else {
    tot = k * wm   bwm;
  }
  document.getElementById("tot").value = "RM  "   tot;
}
<form id="theform" action="#">
  <div>
    <label for="place">Choose Destination :</label>
    <select id="place" onChange="handleClick()">
      <option value="swk">Sarawak</option>
      <option value="sbh">Sabah</option>
      <option value="wm">WestMalaysia</option>
    </select>
  </div>
  <div>
    <label for="kilo">Amount of KG :</label>
    <input id="kilo" type="text">
  </div>
  <div>
    <label>Total :</label>
    <input id="tot" type="text" readonly="readonly">
  </div>
  <div>
    <label></label>
    <input id="calc" type="button" value="calculate">
    <input id="r" type="reset" value="clear">
  </div>
</form>

CodePudding user response:

I Removed the left-hand side for assignment and set the value. You defined the var for all instead of that you can use const. Also form reset not required.

Here is solution of your code

<html>
    <script type="text/javascript">
        const bwm = 7.9;
        const bswk = 14;
        const bsbh = 15;
        const wm = 2;
        const swk = 11;
        const sbh = 12;
        let kilo, overkilo, f;
       
        var tot;

        function quote() {
        const s = document.getElementById('place');
        const place = s.options[s.selectedIndex].value;
        const k = document.getElementById('kilo').value;
            f = document.getElementById('theform');
            // f.reset();
            document.getElementById('calc').onclick = function() {
                if (place == 'swk') {
                   tot = (k * swk)   bswk;
                } else if (place == 'sbh') {
                   tot = (k * sbh)   bsbh;
                } else {
                    tot = (k * wm)   bwm;
                }
                
                
                document.getElementById('tot').value = 'RM '   parseFloat(tot);
            }

        }
    </script>

    <form id="theform" action="#">
        <div>
            <label for="place">Choose Destination :</label>
            <select id="place" onChange="quote()">
                <option value="swk">Sarawak</option>
                <option value="sbh">Sabah</option>
                <option value="wm">WestMalaysia</option>
            </select>
        </div>
        <div>
            <label for="kilo">Amount of KG :</label>
            <input id="kilo" type="text">
        </div>
        <div>
            <label>Total :</label>
            <input id="tot" type="text" readonly="readonly">
        </div>
        <div>
            <label></label>
            <input id="calc" type="submit" value="calculate">
            <input id="r" type="reset" value="clear">
        </div>
    </form>
</html>

  • Related