Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Web Design, HTML And CSS Forums (http://www.go4expert.com/forums/web-design-forum/)
-   -   Total beginner needs help with IF Else (http://www.go4expert.com/forums/total-beginner-help-t17473/)

simp 12May2009 05:11

Total beginner needs help with IF Else
 
Hi

I have only been playing with HTML/JS for a couple of days.

Please can someone tell me why the following statements do not work.

Thanks
HTML Code:

<html>

    <head>
    <title>My Website Page 2</title>
    <script type="text/javascript">
    function sendalert(x)       
    if (x=="Dave")
    {
    document.write("Yourname is Dave");
    }
    else
    {
    document.write("You're not Dave");
    }       
    </script>
    </head>

    <body>
   
    <form>
    <input type=text name="text" value="NAME">
        <input type=button value="click Me" name="send" onclick="sendalert(form.text.value)">
    </form>

    </body>


</html>


SaswatPadhi 12May2009 07:38

Re: Total beginner needs help with IF Else
 
Haha :rofl:
Your func def is not wrapped inside {}.

The working code (tested) is :
Code: html

<html>
      <head>
            <title>My Website Page 2</title>
            <script type="text/javascript">
                  function sendalert(x)
                  {
                        if (x=="Dave")
                              document.write("Yourname is Dave");
                        else
                              document.write("You're not Dave");
                  }
            </script>
      </head>
      <body>
            <form>
                  <input type=text name="text" value="NAME">
                  <input type=button value="click Me" name="send" onclick="sendalert(form.text.value)">
            </form>
      </body>
</html>

Just ignore the hyperlinks, they are automatically added for known tags.

PS : Take care of the following points :
(1) Please post your code inside [code] ... [/ code] (no space between / and code, i gave it to stop recognizing it as code-block)
(2) Please indent your code properly, so that it would be more readable and can be debugged easily. I think, if you had indented your code, you would have easily found the mistake.

simp 12May2009 21:52

Re: Total beginner needs help with IF Else
 
Many thanks for your response.

However I am a little confused now. I have been using the w3schools site to learn and that describes the IF THEN in the way I have coded it. Is the tutorial on their site wrong?

simp 13May2009 00:59

Re: Total beginner needs help with IF Else
 
Ah I see it now , I needed the {} for the function as well as the actions in the IF statement

SaswatPadhi 13May2009 07:09

Re: Total beginner needs help with IF Else
 
You need to close the actions of the IF statement in {}, if they contain more than one instructions.

This is perfectly fine :
Code: java

if (x=="Dave")
      document.write("Yourname is Dave");

But, this :
Code: java

if (x=="Dave")
      document.write("Yourname is Dave");
      document.write("Welcome Dave !");

will not function as expected from the indentation of the code. It will function similar to :
Code: java

if (x=="Dave")
      document.write("Yourname is Dave");
document.write("Welcome Dave !");

So, ifor the correct functioning, you must close both statements within {}.
Code: java

if (x=="Dave")
{
      document.write("Yourname is Dave");
      document.write("Welcome Dave !");
}


I hope, you got it. :smile:


All times are GMT +5.5. The time now is 22:00.