0

The scenario, I have several JavaScript calculations (based on user input) on my page. The results of which are displayed via <span id="x"></span> for example..

document.getElementById("x").innerHTML = +(Math.round(document.form.input.value /25.4 + "e+2")  + "e-2");

My question is how best to display the result if I want to use it again later in the same page.

I have tried just using <span id="x"></span> again later in the page but because the "id" is the same this doesn't work so my current workaround is just to simply repeat the JavaScript calc again and then call it again with "2" on the end like so..

<span id="x2"></span>

document.getElementById("x2").innerHTML = +(Math.round(document.form.input.value /25.4 + "e+2")  + "e-2");

but this has resulted in a lot of repeated/unnecessary extra calculations and I can't help but think there must be a simpler way to repeat the result later on in the same page?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Glen Keybit
  • 296
  • 2
  • 15

2 Answers2

0

create unique IDs in different span elements, create a function and the pass the IDs.

function fun(id) {
    document.getElementById(id).innerHTML = +(Math.round(document.form.input.value /25.4 + "e+2") + "e-2");
}
Tom O.
  • 5,730
  • 2
  • 21
  • 35
0

then anywhere you want to trigger the function, use func(x2)