0

I was wondering instead of using the alert function to show the function result if there was a way to print it in a text field on the same page underneath the Calculate Button. Here is what I have so far:

<!DOCTYPE html>
<html>
<head>
	<title></title>
	<link rel="stylesheet" type="text/css" href="styles/formula_styles.css">

	<script type="text/javascript">

		var a,b,c;
		function setValues1()
		{
			a = Number(document.getElementById("a").value);
			b = Number(document.getElementById("b").value);
			c = Number(document.getElementById("c").value);
		}

		function and()
		{
			setValues1();
			result1 = (-b + Math.sqrt(b*b -4*a*c))/(2*a);
			result2 = (-b - Math.sqrt(b*b -4*a*c))/(2*a);
			alert("The volume of this cube is " +result1 + " and " +result2);
		}

	</script>

</head>
<body>

<nav>
	<a href="index.html">Home</a> // <a href="levels.html">Grade Levels</a>
</nav>

<div id="container">
	<div id="formula">	
		<input type="text" id="a" placeholder="'A' Variable"/><br>
		<input type="text" id="b" placeholder="'B' Variable"/><br>
		<input type="text" id="c" placeholder="'C' Variable"/><br>
		<input type="button" onclick="and()" value="Calculate!"/>
	</div>

</div>
</body>
</html>

1
  • You may need to state what the problem you have. Commented May 28, 2015 at 1:13

3 Answers 3

1

add an element to your html, something like this:

<div id="results"></div>

Then instead of using an alert, you can do something along the lines of this:

document.getElementById("results").innerHTML = "The volume of this cube is " +result1 + " and " +result2;
Sign up to request clarification or add additional context in comments.

Comments

0

Create a div under the button and populate the innerHTML of the div:

<!DOCTYPE html>
<html>

<head>
  <title></title>
  <link rel="stylesheet" type="text/css" href="styles/formula_styles.css">

  <script type="text/javascript">
    var a, b, c;

    function setValues1() {
      a = Number(document.getElementById("a").value);
      b = Number(document.getElementById("b").value);
      c = Number(document.getElementById("c").value);
    }

    function and() {
      setValues1();
      result1 = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
      result2 = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
      document.getElementById('result').innerHTML = "The volume of this cube is " + result1 + " and " + result2;
    }
  </script>

</head>

<body>

  <nav>
    <a href="index.html">Home</a> // <a href="levels.html">Grade Levels</a>
  </nav>

  <div id="container">
    <div id="formula">
      <input type="text" id="a" placeholder="'A' Variable" />
      <br>
      <input type="text" id="b" placeholder="'B' Variable" />
      <br>
      <input type="text" id="c" placeholder="'C' Variable" />
      <br>
      <input type="button" onclick="and()" value="Calculate!" />
      <div id="result">

      </div>
    </div>

  </div>
</body>

</html>

Comments

0

You can do this by setting up an element you wish to contain the text.

document.getElementById("result").innerHTML = "The volume of this cube is " +result1 + " and " + result2;

To do this you need set the element up with an ID of result.

This line of code is calling the function getElementByID to get the element, which then lets you change its innerHTML property.

The element you assign to this can be whatever you want it to be.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.