Question

So i'm kinda new to javascript and am having an issue with the target heart rate calculator I am trying to create. When I click the calculate button, nothing happens. Can anyone tell me where I'm going wrong?

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"lang="en">
<head>
<link href='http://fonts.googleapis.com/css?family=Nixie+One' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/workitt.css"> 
<script>
    function initTH(){
        document.getElementById("calcButton").onclick = thr;
    }
    function thr(){
        var age = document.getElementById("age").value;
        if(age = ""){
            document.getElementById("error").innerHTML = "This field is required.";
        }
        else{
            var THRmax = (220-age)*.85;
            var THRmin = (220-age)*.7;
            document.getElementById("THRzone").innerHTML = THRmin + "-" + THRmax;
        }
    }
</script>
<title>Workitt</title>
</head>
 <body>
    <CENTER><div class="header">
    <h1><img src="images/workitt-header.jpg" alt=header ></h1>
       <div class="navbar">
        <a href="workitt.html">Home</a> |
        <a href="profile.html">Profile</a> |
        <a href="createworkout.html">Create&nbsp;A&nbsp;Workout</a> |
        <a href="accessories.html">Fitness&nbsp;Accessories</a>
  </div>
  <p>&nbsp;</p>
  </div></CENTER>   

  <CENTER><div class="body">
  <form>
        <table class="table1" border="7" bordercolor=WHITE width="250" align="center" bgcolor="#ffffff">
          <thead>
            <tr>
              <th colspan="2"><font size=5>
                Target Heart Rate</font>
              </th>
            </tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
          </thead>
          <tbody>
            <tr>
              <td align="center">
                <label for="age">Age:</label>
              </td>
              <td align="center">
                <input type="text" id="age" name="age" size="6"> years
              </td>
            </tr>
            </tbody>
            <thead>
            <tr>
              <th colspan="2" align="center">
                <input id="calcButton" type="button" value="calculate" />
                <span id="error"></span>
              </th>
            </tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>
                <th colspan="2">
                    Your Target Heart Rate Zone is:&nbsp;<span id="THRzone"></span>
                </th>
            </tr>
        </thead>
    </table>
 </form> 
 <p>*Your target heart rate zone is the zone in which your heart rate should be in when exercising to have the most effective workout for improving your fitness and burning calories.</p> 
</div></CENTER>
</body>
</html>
Était-ce utile?

La solution

The problem with your script, as pointed out in the comments, is that you are never invoking initTH(), so the button click handler is never getting attached to the button.

What the comments fail to note is that you have your script placed before the HTML. In particular, it comes before <input id="calcButton" type="button" value="calculate" />, meaning that if you're not careful when you call document.getElementById("calcButton"), that will return null.

It sounds like what you want to do is add an additional <script> element at the end of the body (right before the close </body> tag) and invoke initTH():

    ....
    <script>
        initTH();
    </script>
</body>

Autres conseils

As others have mentioned, the initTH() function never gets called.

I've made a few changes here: http://jsfiddle.net/GaryHarrower/j4v7M/

I removed the function, so it will run whenever the button is pressed.

The script also had age = "" this should be age == "" with 2 = signs

Let me know how you get on!

    <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"lang="en">
    <head>
        <link href='http://fonts.googleapis.com/css?family=Nixie+One' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" type="text/css" href="css/workitt.css"> 
        <title>Workitt</title>
    </head>
    <body>
        <CENTER>
            <div class="header">
                <h1><img src="images/workitt-header.jpg" alt=header ></h1>
                <div class="navbar">
                    <a href="workitt.html">Home</a> |
                    <a href="profile.html">Profile</a> |
                    <a href="createworkout.html">Create&nbsp;A&nbsp;Workout</a> |
                    <a href="accessories.html">Fitness&nbsp;Accessories</a>
                </div>
                <p>&nbsp;</p>
            </div>
        </CENTER>   

        <CENTER>
            <div class="body">
                <form>
        <table class="table1" border="7" bordercolor=WHITE width="250" align="center" bgcolor="#ffffff">
          <thead>
            <tr>
              <th colspan="2"><font size=5>
                Target Heart Rate</font>
              </th>
            </tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
          </thead>
          <tbody>
            <tr>
              <td align="center">
                <label for="age">Age:</label>
              </td>
              <td align="center">
                <input type="text" id="age" name="age" size="6"> years
              </td>
            </tr>
            </tbody>
            <thead>
            <tr>
              <th colspan="2" align="center">
                <input id="calcButton" type="button" value="calculate" />
                <span id="error"></span>
              </th>
            </tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>&nbsp;</tr>
            <tr>
                <th colspan="2">
                    Your Target Heart Rate Zone is:&nbsp;<span id="THRzone"></span>
                </th>
            </tr>
        </thead>
    </table>
 </form> 
 <p>*Your target heart rate zone is the zone in which your heart rate should be in when exercising to have the most effective workout for improving your fitness and burning calories.</p> 
</div></CENTER>
<script>
            document.getElementById("calcButton").onclick = thr;

            function thr(){
                var age = document.getElementById("age").value;
                if(age == ""){
                    document.getElementById("error").innerHTML = "This field is required.";
                }
                else
                {
                    var THRmax = (220-age)*.85;
                    var THRmin = (220-age)*.7;
                    document.getElementById("THRzone").innerHTML = THRmin + "-" + THRmax;
                }
            }
        </script>
</body>
</html>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top