HTML form with javascript gets "NaN" in IE but works in Chrome

I am by no means a programmer, but I managed to get this to work in Chrome, but I get "NaN" when I try to use IE (11 if that matters). It basically takes 4 criteria and does some math to get a quote. Can anyone help with this?

<html>
<head>
<title>instant inspection quote tool</title>
<SCRIPT Language="JavaScript">

function calculateFee(frm){

   var building     = frm.building.value
   var distance     = frm.distance.value
   var age          = frm.age.value
   var sqft         = frm.sqft.value
   var total        = 0

   building = Number(building)
   distance = Number(distance)
   age      = Number(age)
   sqft     = Number(sqft)
   total    = Number(total)

   total = building + distance + age + sqft
   frm.total.value = total

}

</script>


</head>

<body>

<h1>Inspection Fee Calculator</h1>

<form method="post" action="">

Select the type of home
          <br><input type="radio" name="building" value="375"> detached
          <br><input type="radio" name="building" value="350"> semi-detached
          <br><input type="radio" name="building" value="350"> condo or freehold townhome - end unit
          <br><input type="radio" name="building" value="325"> condo or freehold townhome - interior unit
          <br><input type="radio" name="building" value="299"> condo apartment

<br><br>Is the home within 50 kms of Ottawa?   
          <br><input type="radio" name="distance" value="0"> yes
          <br><input type="radio" name="distance" value="25"> no

<br><br>Is the home less than 50 years old?    
          <br><input type="radio" name="age" value="0"> yes
          <br><input type="radio" name="age" value="25"> no

<br><br>Is the home less than 2000 square feet?    
          <br><input type="radio" name="sqft" value="0"> yes
          <br><input type="radio" name="sqft" value="25"> no
<br><br>

<input type="button" name="button" value="Calculate Fee" onClick="calculateFee(this.form)"> 

<table>
    <tr>
      <td align="right">Total Inspection fee:</td>
      <td>
        <input type="text" name="total" size="30" maxlength="30">
      </td>
    </tr>

</table>

</form>
</body>
</html>

      

+3


source to share


3 answers


Try this - note: querySelector and querySelectorAll don't work in IE <8 and 8 only in standards mode

Live Demo

function calculateFee(frm) {
  var total = 0,
      rads  = frm.querySelectorAll('input:checked'); // get all checked radios
  for (var i = 0; i < rads.length; i++) {
    var num = Number(rads[i].value);
    total += isNaN(num) ? 0 : num;
  }
  frm.total.value = total;
}

      

For individual radio values, use a name - however, getting the value directly occurs if the radio is not checked



var building = frm.querySelector("input[name=building]:checked");
if (building) total += Number(building.value);

      

To check, connect the onsubmit form

function validate(frm) {
  var rads  = frm.querySelectorAll('input:checked'); // get all checked radios
  if (rads.length<4) {
    alert("Please select one of each");
    return false; // cancel form
  }
  return true;
}    
window.onload=function() {
  document.forms[0].onsubmit=function() {
    return validate(this);
  }
}

      

+3


source


If none of the values ​​are "off" then the values ​​are undefined try adding this ...

function getRadioValue( radioElement ){
    for (var i=0; i < radioElement.length; i++){
       if ( radioElement[i].checked ){
          return radioElement[i].value;
       }
    }
}

      



Replace the previous code with this ...

 var building     = getRadioValue( frm.building ) || 0;
   var distance     = getRadioValue( frm.distance ) || 0;
   var age          = getRadioValue( frm.age ) || 0;
   var sqft         = getRadioValue( frm.sqft ) || 0;
   var total        = 0

      

+1


source


Try the following:

var building     = frm.querySelector("[name=building]:checked");
var distance     = frm.querySelector("[name=distance]:checked");
var age          = frm.querySelector("[name=age]:checked");
var sqft         = frm.querySelector("[name=sqft]:checked");

building = (building && building.value) || 0;
distance = (distance && distance.value) || 0;
age = (age && age.value) || 0;
sqft = (sqft && sqft.value) || 0;

      

0


source







All Articles