Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
10
rated 0 times [  12] [ 2]  / answers: 1 / hits: 75282  / 12 Years ago, fri, june 29, 2012, 12:00:00

I am trying to make a BMI Calculator using HTML. The inputs come from text fields. I am making some calculation with them in a Javascript function. I want to get the calculated values in a span like this:



<span id= class=>Your Calculation Results is = </span>
<span id= class=> I want the result here </span>


====================================================================
Edit : The part that i am asking about



<script language=javascript>
var bmi = document.getElementById(ID_bmiResult2).innerHTML;
bmi.value = weight / (heightInMeter * heightInMeter);
</script>

<body>
<span id=ID_bmiResult2> </span>
</body>


i don't know how to make it ...


More From » html

 Answers
24

Edit: As it was pointed out, FF doesn't like innerText, so I'm replacing to innerHTML which in this particular case doesn't not change the overall functionality of the script.



You can get the 2nd span and pass the value directly to innerHTML like this:



document.getElementsByTagName('span')[1].innerHTML= 'the result';


Note: Consider using and id or class so you can find the exact element you need to change instead of retrieving several elements of that type (in this case span).
If you had an id, it would be like this:



document.getElementById('the id goes here').innerHTML= 'the result';


Edit2: This edit was made after the OP changed his question
It got really confusing now that I'm seeing your code.
I've written this fiddle so you can see it working


[#84563] Thursday, June 28, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leog

Total Points: 225
Total Questions: 113
Total Answers: 118

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;