Search code examples
javascripthtmlequation

Is it possible to use a JavaScript in simple math equation but place results in a div


I'm trying to put the results of this simple JavaScript into a div, where i put the results anywhere on the webpage

Example script:

var numOne=25
  , numTwo=14
  , res
  ; 
res = numOne - numTwo; 
document.write(" " + res + "");

I don't have a clue how to set this up in the script


Solution

  • Create a div

    It is best if it already exists in the HTML and has an id, as shown below.

    Use document.getElementById to access that element, and put the result into its innerText

    Here is an example.

    var numOne = 25,
      numTwo = 14,
      res;
    res = numOne - numTwo;
    
    document.getElementById("result").innerText = res;
    <div id="result"></div>