Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-16-2013, 09:33 PM   PM User | #1
ahaberman25
New Coder

 
Join Date: Jan 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
ahaberman25 is an unknown quantity at this point
Having issues with BMI calculator

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.

code found here

berlin test.html is the quiz version
and berlinbmi.html is the stand alone version.

I have got to be missing something and just cannot figure it out. please help.
ahaberman25 is offline   Reply With Quote
Old 01-17-2013, 12:41 AM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by ahaberman25 View Post
I have got to be missing something
Yes - use of the error console.

If a function addresses a form, pass it a reference to the form, don't address the form via a changeable index.
Logic Ali is offline   Reply With Quote
Old 01-17-2013, 12:44 AM   PM User | #3
ahaberman25
New Coder

 
Join Date: Jan 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
ahaberman25 is an unknown quantity at this point
I am sorry but I dont understand

error is saying undefined value f.wt.value

Last edited by ahaberman25; 01-17-2013 at 01:49 AM..
ahaberman25 is offline   Reply With Quote
Old 01-17-2013, 01:48 AM   PM User | #4
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by ahaberman25 View Post
I am sorry but I dont understand
Quote:
Code:
function compute(){
   var f = self.document.forms[1];
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 );"
Logic Ali is offline   Reply With Quote
Users who have thanked Logic Ali for this post:
ahaberman25 (01-17-2013)
Old 01-17-2013, 01:52 AM   PM User | #5
ahaberman25
New Coder

 
Join Date: Jan 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
ahaberman25 is an unknown quantity at this point
Quote:
Originally Posted by Logic Ali View Post
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!
ahaberman25 is offline   Reply With Quote
Old 01-17-2013, 01:56 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You are using
Code:
var f = self.document.forms[1];
on both pages.

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");
__________________
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.
Old Pedant is offline   Reply With Quote
Old 01-17-2013, 01:57 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
And LogicAli's answer of using onclick="self.compute( this.form );" is even better. By far the best solution.

But in any case, it is way way past time to stop using named forms and move to using id's.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 01-17-2013, 02:00 AM   PM User | #8
ahaberman25
New Coder

 
Join Date: Jan 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
ahaberman25 is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
You are using
Code:
var f = self.document.forms[1];
on both pages.

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)

Thank you for your answer.
ahaberman25 is offline   Reply With Quote
Old 01-17-2013, 02:17 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I still like LogicAli's answer better.

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.
Old Pedant is offline   Reply With Quote
Old 01-17-2013, 02:18 AM   PM User | #10
ahaberman25
New Coder

 
Join Date: Jan 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
ahaberman25 is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
I still like LogicAli's answer better.

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.
ahaberman25 is offline   Reply With Quote
Old 01-17-2013, 03:03 AM   PM User | #11
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by ahaberman25 View Post
doesnt work, the var f is called through out almost every other variable, i would have to re structure some of it.
WHAT?? NOT TRUE!

Here:
http://mywhizbang.com/bmi.html

Go ahead. Do a VIEW-->>SOURCE to see what I did.

But all I changed was
Code:
<input type="button" value="Compute BMI" onclick="compute(this.form);" />
and
Code:
function compute(f){
   // and I removed your var f = self.document.forms[4];
Note that this change *HAD* to work, because of the var in that line I removed!

*BY DEFINITION* the variable f is ONLY LOCAL to that one function!

Any time you declare a var *inside* a function, it can only be seen within that function.

So your statement that
Quote:
var f is called through out ...
is simply not true. The *ONLY* place *THAT PARTICULAR* var f can be used is *inside* that compute( ) function.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 01-17-2013, 03:07 AM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 01-17-2013, 03:23 AM   PM User | #13
ahaberman25
New Coder

 
Join Date: Jan 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
ahaberman25 is an unknown quantity at this point
Strange when I do it, says "cannot find variable f"
ahaberman25 is offline   Reply With Quote
Old 01-17-2013, 03:23 AM   PM User | #14
ahaberman25
New Coder

 
Join Date: Jan 2013
Posts: 19
Thanks: 2
Thanked 0 Times in 0 Posts
ahaberman25 is an unknown quantity at this point
nevermind im retarded you added compute(f)
ahaberman25 is offline   Reply With Quote
Old 01-17-2013, 03:33 AM   PM User | #15
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
LOL! Yep, no wonder you were confused!
__________________
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:15 PM.


Advertisement
Log in to turn off these ads.