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.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Running If Else with a User Input

    Hey, so i've been learning basics and I can get the following code to work as a prompt and I thought hey, I wounder if I can get it to work of a UI system, so I edited the code and got this

    Read Below Post

    Can you please help me to find where I am going wrong.
    Thanks
    MancunianMacca
    Last edited by MancunianMacca; 03-04-2012 at 08:02 PM.

  • #2
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Ok so I have been experimanting more and have got it to recognise the user input but now it only displays the else option

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    <script type="text/javascript">
    function testing1(){
    var testing
    if (testing1 == "yes")
    {
    	alert("Yay!");
    }
    else
    {
    	alert("Yes it does");
    }
    }
    </script>
    <input type="text" onchange="testing1()" />
    </body>
    </html>

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    You must pass the value entered by the user to the function. It does not recognise the user input, but simple alerts the default.
    var testing // this variable is not used
    if (testing1 == "yes") // testing1 is the name of the function, not a variable.

    Code:
    <script type="text/javascript">
    
    function testing1(testvalue){  // pass the entered value to the function
    if (testvalue.toLowerCase()== "yes") {  // make case-insensitive
    alert ("Yay!");
    }
    else{
    alert ("Yes it does");
    }
    }
    </script>
    <input type="text" onchange="testing1(this.value)" />
    </body>
    </html></body>
    </html>

    Quizmaster: What is the Latin name of the province in Canada which means "New Scotland" in English?
    Contestant: Johannesburg.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Works perfectly but so I am actually learning not just copying ould you please ecplain what the (this.value) does

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    onchange="testing1(this.value)"

    When the value of the textbox is changed and loses focus, run the function testing1() and pass to that function the value of this element (the current object). In other words, the value that the user entered. OK?
    Last edited by Philip M; 03-04-2012 at 03:26 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    New Coder
    Join Date
    Feb 2012
    Location
    England
    Posts
    59
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Philip M View Post
    onchange="testing1(this.value)"

    When the value of the textbox is changed and loses focus, run the function testing1() and pass to that function the value of this element (the current object). In other words, the value that the user entered. OK?
    I'm honestly not trying to be annoying but i interpreted what you just said as " when the user has typed their answer, they can click off the text box and the function will run." am I right??
    Last edited by MancunianMacca; 03-04-2012 at 04:17 PM.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by MancunianMacca View Post
    I'm honestly not trying to be annoying but i interpreted what you just said as " when the user has typed their answer, they can click off the text box and the function will run." am I right??
    Yes. But I do think you ought to buy a book. This forum is not intended for remedial teaching.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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