Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    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.

  • #3
    New Coder
    Join Date
    Jan 2013
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by ahaberman25 View Post
    I am sorry but I dont understand
    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 );"

  • Users who have thanked Logic Ali for this post:

    ahaberman25 (01-17-2013)

  • #5
    New Coder
    Join Date
    Jan 2013
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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!

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    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.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    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.

  • #8
    New Coder
    Join Date
    Jan 2013
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    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.

  • #10
    New Coder
    Join Date
    Jan 2013
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    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
    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.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    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.

  • #13
    New Coder
    Join Date
    Jan 2013
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Strange when I do it, says "cannot find variable f"

  • #14
    New Coder
    Join Date
    Jan 2013
    Posts
    31
    Thanks
    2
    Thanked 0 Times in 0 Posts
    nevermind im retarded you added compute(f)

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •