Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading