I have added code to a quiz that I am building to include a BMI calculator and it will not display the result. I have built it on a separate page and it works fine, when I add it to the quiz it fails.
How do you know that the relevant form will always be the second form in the document?
(Changing the index isn't the answer)
Code:
function compute( theForm )
{
var w = theForm.wt.value,
v = theForm.htf.value,
u = theForm.hti.value;
.........
onclick="self.compute( this.form );"
OMFG that totally makes sense, I didnt see the stupid [1].....i changed to 4 and BAM...i just didnt see it, i knew it was something so retarded gawd. Been doing this for 2 years and still run into issues. THANK YOU VERY MUCH!
But on the main page, the BMI form is *actually* forms[4].
It's a really crappy idea to use NUMBERED forms.
And NAMED forms are considered VERY obsolete.
You should give IDs to all your <form> tags!
So:
Code:
<form id="bmi_input">
and then
Code:
var f = document.getElementById("bmi_input");
for this project I am just changing to [4] which i figured out from ali's last post, I will be using id's but i had permission to use this code so I was skipping a step and using a prev employees code (which is always hell)
It has no impact on the form name (or lack thereof). It affects only the calling of the BMI function. It doesn't depend on what form "number" you are using. etc., etc., etc.
You can leave all the code in the function the same if you just use
Code:
function compute( f )
{
// remove the line: var f = ...
... rest of code untouched ...
}
... onclick="self.compute( this.form );" ...
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
It has no impact on the form name (or lack thereof). It affects only the calling of the BMI function. It doesn't depend on what form "number" you are using. etc., etc., etc.
You can leave all the code in the function the same if you just use
Code:
function compute( f )
{
// remove the line: var f = ...
... rest of code untouched ...
}
... onclick="self.compute( this.form );" ...
doesnt work, the var f is called through out almost every other variable, i would have to re structure some of it.
I understand that mucking in other people's code can be a pain, but if you are going to have to muck, you might as well do some "mucking out". Toss out code that is clearly problematic.
Just for example, suppose somebody *else* comes along after you and adds *another* <form> into that page. So that now the BMI form is actually forms[5] !! That person will have to struggle with finding that wrongly numbered <form> all over again! Whereas if you use LogicAli's answer (or even my answer!) the code is now fixed so that this confusion won't have to occur again.
I hope you won't take this wrong, but if you were on my team and just changed the [1] to [4] you would be stomped all over in a code review!! (And not just by me! Every team member would call you out.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.