Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Javascript if statement

Options
  • 07-01-2005 11:27pm
    #1
    Registered Users Posts: 884 ✭✭✭


    Lads, i convert a string with

    variable1 = parseFloat(document.form.textfield.value);

    then i pass variable1 to a function

    functionname(variable1)

    and then i check

    if (variable1 >= 100)
    {
    alert("it works");
    }
    else if (variable1>=50)
    {
    alert("it works aswell");
    }
    else (variable1 >= 25)
    {
    alert("it works all the time");
    }


    and then

    whatever = functionname

    Why doesn't it work ??
    It wont go through the if statement ???


Comments

  • Registered Users Posts: 15,258 ✭✭✭✭Rabies


    Should it be

    functionname(variable1)

    and then i check

    if (variable1 >= 100)
    {
    alert("it works");
    }
    else if (variable1>=50)
    {
    alert("it works aswell");
    }
    else if (variable1 >= 25)
    {
    alert("it works all the time");
    }
    else
    {
    whatever = functionname;
    }


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    No ... it is a javascript script in html ... not php

    It is just the if statement is not check the number that it is being given

    if the condition is true it doesn't execture the alert


    if (variable1 >= 100)
    {
    alert("it works");
    }


  • Registered Users Posts: 236 ✭✭richardo


    Probably a silly question, but have you declared variable1 as a variable?

    variable1 = parseFloat(document.form.textfield.value);

    should be

    var variable1 = parseFloat(document.form.textfield.value);


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    richardo wrote:
    Probably a silly question, but have you declared variable1 as a variable?

    variable1 = parseFloat(document.form.textfield.value);

    should be

    var variable1 = parseFloat(document.form.textfield.value);

    Yeah ..... like this

    var variable1;

    variable1 = parseFloat(document.form.textfield.value);


    that'll do wont it ?


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    Lads, the same piece of code but a different problem

    I am getting a mesage saying that line 101, the text field document.results.comorgper.value is null or not an object ...

    it is an object because i have had it working previously (something written to it) and this piece of code check if it has something in it ...


    if (document.results.comorgper.value=="")
    {
    alert("Please enter a percentage result");
    document.results.comorgper.focus();
    }



    Actually ... i would mind mailing it to someone to have a look at it if possible !?! I'll be at it all today (sun)


  • Advertisement
  • Registered Users Posts: 884 ✭✭✭Cork Skate


    I have created a new page and still get errors ....

    <html>
    <head><title>Test Page 106</title></head>
    <script language="JavaScript">
    function testif()
    {
    var number = 0,
    var letter = 5;
    if (number = 0)
    {
    alert("Thisi s the first part of the if");
    if (letter = 5)
    {
    alert("Thisi s the second part of the if");
    }
    }
    }
    </script>
    <body>
    <input type="button" value="Submit" onClick="testif()">
    </body>
    </html>


    It now says that ...

    line 7 char 2 Expected Identifier
    line 19 char 1 Object Expected


  • Registered Users Posts: 236 ✭✭richardo


    Cork Skate wrote:
    I have created a new page and still get errors ....



    It now says that ...

    line 7 char 2 Expected Identifier
    line 19 char 1 Object Expected

    Two problems there:
    var number=0, should be var number =0; [semi-colon, not comma]

    and when testing for equality, use '==' NOT '='
    so change 'if (number=0)' to 'if (number==0)' [and of course the test for 'letter']

    Hope that helps.........


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    Ricardo ... thanks a million. I am totally new to javascript so am learning as i go along. !! Cheers again !!


  • Registered Users Posts: 236 ✭✭richardo


    Glad to be of some help. We ALL had to start somewhere. And if you don't try, you'll get nowhere fast!


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    Ricardo ... thisis probably obvious but when that if is finished ... it does move to the next one right .... i dont have to return; at the end of the if i am guessing ??


  • Advertisement
  • Registered Users Posts: 236 ✭✭richardo


    At the end of an IF condition, the next statment will be processed.
    In your example, you have nested the IFs so if number != 0 then that is the end of the function. The test for 'letter' will only occur if number does equal 0.
    I always like to indent my code. It makes debugging a LOT easier:
    function testif()
       {
       var number = 0;
       var letter = 5;
       if (number == 0)
          {
           alert("This is the first part of the if");
           if (letter == 5)
              {
              alert("This is the second part of the if");
              }
         }
      }
    
    Any help??


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    Yeah, its good .... but i have another problem now ...

    if (document.results.comorgche.checked==true)
    {
    if (document.results.comorgper.value=="")
    {
    alert("Please enter a percentage result");
    document.results.comorgper.focus();
    }

    it is saying that document.results.comorgper.value is null or not an object

    but it worked fine when i had

    if (document.results.comorgche.checked==true && document.results.comorgper.value=="")


  • Registered Users Posts: 884 ✭✭✭Cork Skate


    Sorry ... i had the
    </form>
    
    in the wrong place ... oopps

    It works fine now !!


Advertisement