0

So I taught myself coding a few years ago, and got it just enough to put together a few tools for work. I recently had to migrate my site out of CodePen and onto an actual web server. Now I'm having an issue where part of my javascript is executing properly (a portion that empties all other input fields when a user enters an input field using JQuery), but the button that calculates an answer will not work. I believe the .click is not picking it up. Either way I'm not getting error messages, the button just does nothing when I press it.

When I put the code in a snippet to share with you guys, it works (just like it did in CodePen), but the exact same code on my web host does not work. I'm really at a loss here and any help would be greatly appreciated. I feel like I'm missing some small line of code that's supposed to be included in all web files.

$(document).ready(function() {
  //Clear out input fields when not selected
  $("#sg").focusin(function() {
    $("#density").val("");
  });
  $("#density").focusin(function() {
    $("#sg").val("");
  });
  $("#pounds").focusin(function() {
    $("#grams").val("");
    $("#percentage").val("");
  });
  $("#grams").focusin(function() {
    $("#percentage").val("");
    $("#pounds").val("");
  });
  $("#percentage").focusin(function() {
    $("#pounds").val("");
    $("#grams").val("");
  });
  $(".input_field").focusin(function() {
    $("#density").removeClass('highlight');
    $("#sg").removeClass('highlight');
    $("#pounds").removeClass('highlight');
    $("#grams").removeClass('highlight');
    $("#percentage").removeClass('highlight');
  });
  //Calculate on press of enter
  $("#button").keypress(function(e) {
    if (e.which == 13) {
      alert("this is working");
    }
  });
  $("#button").click(function() {
    calculateButton();

  });

  //Calculate values on button hit
  function calculateButton() {
    function numberWithCommas(x) {
      x = x.toString();
      var pattern = /(-?\d+)(\d{3})/;
      while (pattern.test(x))
        x = x.replace(pattern, "$1,$2");
      return x;
    }

    function removeCommas(x) {
      x = x.replace(",", "");
      return x;
    }

    var results = 0;
    //Pulling information from input cells
    var densityStr = document.getElementById("density").value;
    var sgStr = document.getElementById("sg").value;
    var poundsStr = document.getElementById("pounds").value;
    var gramsStr = document.getElementById("grams").value;
    var percentageStr = document.getElementById("percentage").value;
    //remove commas from string and then convert string to number
    var densityNum = Number(removeCommas(densityStr));
    var sgNum = Number(removeCommas(sgStr));
    var poundsNum = Number(removeCommas(poundsStr));
    var gramsNum = Number(removeCommas(gramsStr));
    var percentageNum = Number(removeCommas(percentageStr));

    if (densityStr.length !== 0) {
      var sgConversion = densityNum / 8.3454;
      $("#sg").val(sgConversion.toFixed(3));
      $("#density").addClass('highlight');
    } else if (sgStr.length !== 0) {
      var densityConversion = sgNum * 8.3454;
      $("#density").val(densityConversion.toFixed(3));
      $("#sg").addClass('highlight');
    }


    if (poundsStr.length !== 0) {
      $("#pounds").addClass("highlight");
      densityNum = document.getElementById("density").value;
      var gramsConversion = poundsNum * 119.83;
      var percentageConversion = poundsNum / densityNum * 100;
      $("#grams").val(gramsConversion.toFixed(0));
      $("#percentage").val(percentageConversion.toFixed(2));
    } else if (gramsStr.length !== 0) {
      $("#grams").addClass("highlight");
      densityNum = document.getElementById("density").value;
      var poundsConversion = gramsNum / 119.83;
      var percentageConversion = poundsConversion / densityNum * 100;
      $("#pounds").val(poundsConversion.toFixed(2));
      $("#percentage").val(percentageConversion.toFixed(2));
    } else if (percentageStr.length !== 0) {
      $("#percentage").addClass("highlight");
      densityNum = document.getElementById("density").value;
      var percentageDec = percentageNum / 100;
      var poundsConversion = densityNum * percentageDec;
      var gramsConversion = poundsConversion * 119.83;
      $("#pounds").val(poundsConversion.toFixed(2));
      $("#grams").val(gramsConversion.toFixed(2));
    }
  }
});
body {
  margin: 0;
  font-family: 'Lato', sans-serif;
  background: #d2d2d2;
}

p {
  text-align: center;
}

conatiner {
  max-width: 1024px;
  margin: 0 auto;
}

#navbarContainer {
  background: #F44336;
  overflow: hidden;
  width: 100%;
  margin: 0;
}

.navbar {
  float: left;
  display: block;
  font-family: 'Lato', sans-serif;
  height: 40px;
  width: 200px;
  line-height: 40px;
  text-align: center;
  background: #F44336;
  text-decoration: none;
  color: #212121;
}

.navbar:hover {
  background: #E57373;
  color: white;
}

.active {
  background: #C62828;
  color: white;
}

#formContainer {
  width: 450px;
  background: #FDFFFC;
  margin: 50px auto;
  padding: 0px;
  border-radius: 8px;
  overflow: hidden;
}

#formContainer header {
  width: 100%;
  height: 130px;
  background-color: #3cba54;
  overflow: auto;
  color: white;
}

header h1 {
  margin: 35px 0 0 0;
  text-align: center;
  line-height: 30px;
}

header h3 {
  line-height: 40px;
  text-align: center;
  margin: 0;
}

#heading {
  background-color: #3cba54;
  height: 40px;
  color: white;
  margin-bottom: 25px;
  margin-left: -30px;
}

#heading h3 {
  line-height: 40px;
}

form {
  padding: 20px 0 0 20px;
  text-align: center;
}

label {
  display: inline-block;
  width: 220px;
  text-align: right;
}

#myForm .input_field {
  margin-left: 20px;
  margin-bottom: 10px;
  font-size: 20px;
  padding-left: 10px;
  width: 125px;
  height: 35px;
  font-size: 17px;
  border-radius: 3px;
  background-color: #E0E0E0;
  border: none;
}

#button {
  display: block;
  border-radius: 6px;
  width: 200px;
  height: 50px;
  padding: 8px 15px 8px 15px;
  margin: 0 auto;
  margin-bottom: 50px;
  font-size: 16px;
  box-shadow: 0 6px #540000;
  background-color: #FF3636;
  border: none;
  outline: none;
}

#button:active {
  background-color: #B81B1B;
  box-shadow: 0 1px #27496d;
  transform: translateY(5px);
}

.highlight {
  background: #FFEB3B !important;
}
<!DOCTYPE html>
<html>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <link rel="stylesheet" href="style.css">
  <script type="text/javascript" src="script.js"></script>
</head>

<body>
  <div id="container">
    <div id="navbarContainer">
      <a class="navbar" id="62" href="https://s.codepen.io/awheat/debug/MpMrEo/yYAyLDjQWgKr">326 IAC 6-2 Tool</a>
      <a class="navbar" id="63" href="https://s.codepen.io/awheat/debug/gWmazm/NQkzYnjeQZyA">326 IAC 6-3 Tool</a>
      <a class="navbar active" id="voc" href="https://s.codepen.io/awheat/debug/qVpPNm/VGAWNnJYBjZr">VOC Conversion Tool</a>
    </div>
    <div id="formContainer">
      <header>
        <h1>VOC Conversion Tool</h1>
        <h3>(for conversion of VOC data to other units)</h3>
      </header>
      <form id="myForm">
        <label>Density of Coating (lbs/gal): </label><input type="text" id="density" class="input_field">
        <label>Specific Graviy: </label><input type="text" id="sg" class="input_field">
        <div id="heading">
          <h3>VOC Content</h3>
        </div>
        <label>Pounds per Gallon (lbs/gal): </label><input type="text" id="pounds" class="input_field">
        <label>Grams per Liter (g/L): </label><input type="text" id="grams" class="input_field">
        <label>Percentage (%): </label><input type="text" id="percentage" class="input_field"><br><br>
        <input type="button" id="button" value="Calculate" autofocus>
      </form>
    </div>
  </div>
</body>

</html>
  • 2
    Have you tried using a javascript debugger and stepping through the code? – Alex Wayne Feb 05 '20 at 20:06
  • I embarrassed to say I am out of practice enough I don't remember what exactly this would entail. Is this built into my browser or am I looking at a site or program? Thanks. – Adam Wheat Feb 05 '20 at 20:28
  • 1
    I'd either code with jQuery the whole way through, or not, the whole way through. Mixing jQuery and `document.getElementById` will just lead to bugs as you think a variable refers to a jQuery object, but it's actually a DOM element. And vice versa. Here's a link on debugging in Chrome: https://stackoverflow.com/questions/10638059/javascript-debugging-line-by-line-using-google-chrome – Heretic Monkey Feb 05 '20 at 20:31
  • I think I figured it out. After opening the debugger I realized that despite me updating the javascript file to make the calculate button work properly, the web host was not sending updated files to the web browsers. So every instance of my site was using the old, broken javascript files. Thanks for all the help. – Adam Wheat Feb 05 '20 at 20:51

1 Answers1

0

Sometimes putting script tags before the elements on the page can cause issues. You can try to put the scripts at the bottom of the body like this:

<!DOCTYPE html>
<html>

<head>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div id="container">
    <div id="navbarContainer">
      <a class="navbar" id="62" href="https://s.codepen.io/awheat/debug/MpMrEo/yYAyLDjQWgKr">326 IAC 6-2 Tool</a>
      <a class="navbar" id="63" href="https://s.codepen.io/awheat/debug/gWmazm/NQkzYnjeQZyA">326 IAC 6-3 Tool</a>
      <a class="navbar active" id="voc" href="https://s.codepen.io/awheat/debug/qVpPNm/VGAWNnJYBjZr">VOC Conversion Tool</a>
    </div>
    <div id="formContainer">
      <header>
        <h1>VOC Conversion Tool</h1>
        <h3>(for conversion of VOC data to other units)</h3>
      </header>
      <form id="myForm">
        <label>Density of Coating (lbs/gal): </label><input type="text" id="density" class="input_field">
        <label>Specific Graviy: </label><input type="text" id="sg" class="input_field">
        <div id="heading">
          <h3>VOC Content</h3>
        </div>
        <label>Pounds per Gallon (lbs/gal): </label><input type="text" id="pounds" class="input_field">
        <label>Grams per Liter (g/L): </label><input type="text" id="grams" class="input_field">
        <label>Percentage (%): </label><input type="text" id="percentage" class="input_field"><br><br>
        <input type="button" id="button" value="Calculate" autofocus>
      </form>
    </div>
  </div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>

</html>
VirtualParticle
  • 113
  • 1
  • 6
  • But the whole thing is wrapped in `$(document).ready(function(){})`, which accomplishes the same thing. So I don't think that's the problem. – Teepeemm Feb 05 '20 at 20:10
  • Great idea. Unfortunately it didn't do the trick. I can't figure out why it's just the $("#button").click(function() { calculateButton(); }); doesn't seem to work. – Adam Wheat Feb 05 '20 at 20:14