-3

Sorry if this question sounds pretty stupid;

My situation is that I have three input sliders in my HTML; then, within the <body> tag, after the inputs, I have some JavaScript accessing the data. The unfortunate part is that I want the embedded JavaScript to be run via an onClick event; but, the embedded JavaScript is not one big function, so I'm rather stumped. Does anybody have a solution? Here is my code:

<html>
<body>
<form>
</form>
<form>
<input type="range" id="ocAc"></input>
<input type="range" id="ocBc"></input>
<input type="range" id="ocCc"></input>
</form>
<script>
var oc1 = document.getElementById("ocAc").value / 100;
var oc2 = document.getElementById("ocBc").value / 100;
var oc3 = document.getElementById("ocCc").value / 100;
var total = oc1+oc2+oc3;
//do stuff 
</script>
adrian
  • 1,439
  • 1
  • 15
  • 23

1 Answers1

0

Is this what you wanted?

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="style.css">
  <script src="script.js"></script>
</head>

<body>
  <form>
    <input type="range" id="ocAc" min="100" max="500" step="10" />
    <input type="range" id="ocBc" min="100" max="500" step="10" />
    <input type="range" id="ocCc" min="100" max="500" step="10" />
    <br />
    <button id="myButton">Calculate</button>
    <input type="text" id="myInput" />
  </form>

  <script>
    function modifyText(event) {
      event.preventDefault();
      var oc1 = document.getElementById("ocAc").value;
      var oc2 = document.getElementById("ocBc").value;
      var oc3 = document.getElementById("ocCc").value;
      var total = oc1 + oc2 + oc3;
      document.getElementById('myInput').value = total;
    };

    // add event listener for button.
    var el = document.getElementById("myButton");
    el.addEventListener("click", modifyText, false);
  </script>
</body>
</html>
Latin Warrior
  • 902
  • 11
  • 14