Solution 1 :

The radio button were not working because you were taking them as an ID but the id does not exist in the input. You have get them via getElementsByName()[0]

Also you event listener did not know where button is clicked so the button id is is unique and it will listen to that click only when you click submit.

Here is working demo: https://jsfiddle.net/usmanmunir/tjsnaz4w/10/

function impCalc() {
    var bmrIm = 0;
    var ageIm = document.getElementById("ageinput").value;
    var heightIm = document.getElementById("heightinput").value;
    var weightIm = document.getElementById("weightinput").value;
    var genderIm = document.getElementsByName("gender")[0].value   

    if (genderIm.value == "1") {
        bmrIm = 66 + (6.2 * weightIm) + (12.7 * heightIm) - (6.76 * ageIm);
    }
    else {
        bmrIm = 655 + (4.35 * weightIm) + (4.7 * heightIm) - (4.7 * ageIm);
    }

    (ageIm && heightIm && weightIm) ? alert("Your BMR is: " + bmrIm) : alert("Please fill in all fields");  

}
var el = document.getElementById('submit');
el.addEventListener("click", impCalc, false);

HTML

<form>
        <fieldset id="ImpCalcInfo">
          <label for="ageinput">
            Age
            <input tabindex="1" type="text" id="ageinput" name="age" />
          </label>
          <label for="heightinput">
            Height
            <input tabindex="3" type="text" id="heightinput" name="heigh" />
          </label>
            <label for="weightinput">
            Weight
          <input tabindex="5" type="text" id="weightinput" name="weight" />
          </label>
            <label for="genderinput">
          <input name="gender" tabindex="7" type="radio" id="gender" value="1" checked>Male
          <input name="gender" tabindex="9" type="radio" id="gender" value="0">Female      
            </label>
        </fieldset>        
        <input tabindex="11" type="button" id="submit" value="Submit" />
        <input tabindex="13" type="reset" value="Clear fields" />              
     </form>

Hope this helps.

Problem :

I am doing a simple online form to calculate BMR using Harris–Benedict equation with the imperial measurements. I don’t know where the error is inside my code but right now only the Clear the form button works. I didn’t post the entire HTML code for the webpage because it looks just like I want it and it’s only the calculation that I am having the problem with.

    <form>
        <fieldset id="ImpCalcInfo">
          <label for="ageinput">
            Age
            <input tabindex="1" type="text" id="ageinput" name="age" />
          </label>
          <label for="heightinput">
            Height
            <input tabindex="3" type="text" id="heightinput" name="heigh" />
          </label>
            <label for="weightinput">
            Weight
          <input tabindex="5" type="text" id="weightinput" name="weight" />
          </label>
            <label for="genderinput">
          <input name="gender" tabindex="7" type="radio" id="maleinput" value="1" checked>Male
          <input name="gender" tabindex="9" type="radio" id="femaleinput" value="0">Female      
            </label>
        </fieldset>        
        <input tabindex="11" type="button" id="submit" value="Submit" />
        <input tabindex="13" type="reset" value="Clear fields" />              
     </form>
    function impCalc() {
    var bmrIm = 0;
    var ageIm = document.getElementById("ageinput").value;
    var heightIm = document.getElementById("heightinput").value;
    var weightIm = document.getElementById("weightinput").value;
    var genderIm = document.getElementById("gender").value;     

    if (genderIm.value = "1") {
        bmrIm = 66 + (6.2 * weightIm) + (12.7 * heightIm) - (6.76 * ageIm);
    }
    else {
        bmrIm = 655 + (4.35 * weightIm) + (4.7 * heightIm) - (4.7 * ageIm);
    }

    (ageIm && heightIm && weightIm) ? alert("Your BMR is: " + bmrIm) : alert("Please fill in all fields");  

}
document.getElementById("button").addEventListener("submit", impCalc, false);

Comments

Comment posted by Paul T.

Did you check the browser console for any possible error(s)?

Comment posted by Wael Fadhel

I think the submit button should go this way : becaue you are using this document.getElementById(“button”) while your id is “submit” not “button”

Comment posted by Alan Shore

I haven’t uploaded it on the server yet, it’s running locally on Chrome and I have an error “Failed to load resource: the server responded with a status of 404 (Not Found)”.

Comment posted by Always Helping

Please check the working answer. It working on fiddle and should work on your server as well.

Comment posted by Alan Shore

Wael – I’ve fixed that and the error in Chrome has disappeared. However, the form still doesn’t work, not even on the empty fields. Thanks anyways.

Comment posted by Always Helping

@GregNog You might have an old version of firefox which is not showing alert(). Please uninstall and install again. It should work. if this answer fixed you problem please accept to help other members as well. Thanks

By