Total beginner needs help with IF Else

Discussion in 'Web Design, HTML And CSS' started by simp, May 12, 2009.

  1. simp

    simp New Member

    Joined:
    May 12, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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:
    <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>
     
    Last edited by a moderator: May 12, 2009
  2. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    Haha :rofl:
    Your func def is not wrapped inside {}.

    The working code (tested) is :
    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][/B] (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.
     
    Last edited: May 12, 2009
  3. simp

    simp New Member

    Joined:
    May 12, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  4. simp

    simp New Member

    Joined:
    May 12, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Ah I see it now , I needed the {} for the function as well as the actions in the IF statement
     
  5. SaswatPadhi

    SaswatPadhi ~ Б0ЯИ Τ0 С0δЭ ~

    Joined:
    May 5, 2009
    Messages:
    1,342
    Likes Received:
    55
    Trophy Points:
    0
    Occupation:
    STUDENT !
    Location:
    Orissa, INDIA
    Home Page:
    http://www.crackingforfun.blogspot.com
    You need to close the actions of the IF statement in {}, if they contain more than one instructions.

    This is perfectly fine :
    Code:
    if (x=="Dave")
          document.write("Yourname is Dave");
    But, this :
    Code:
    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:
    if (x=="Dave")
          document.write("Yourname is Dave");
    document.write("Welcome Dave !");
    
    So, ifor the correct functioning, you must close both statements within {}.
    Code:
    if (x=="Dave")
    {
          document.write("Yourname is Dave");
          document.write("Welcome Dave !");
    }
    
    I hope, you got it. :smile:
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice