+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    1

    Basic javascript help with if function

     
    I need to make a simple program where the user inputs into a text box a number, then clicks a button and another text box returns whether the number is greater or less than 20. I know its an if function but not sure what to put. Also does this require parsefloat? Any help at all is greatly appreciated

    This is all I have
    <html>

    <head>
    <title> Compare A Number to 20 </title>
    </head>
    <body>
    <p>Enter a number</p>
    <input type="text" id="num1" value= "" size="10" />
    <br/>
    <input type="button" id="btn" value= "Compare With 20" onClick=" ? " />
    <br/>
    <input type="text" id="btn" size="10" />
    </script>
    </body>
    </html>

  2. #2
    Member
    Join Date
    Mar 2010
    Location
    Ontario
    Posts
    159
    well, first off you're missing a script starting tag

    But basically what you could do is write a javascript method to handle the comparison, and call it as the button's onClick handler

    your method would look something like

    function doSomething () {
    if (document.getElementById('num1').value < 20 ){
    alert ('The value you entered was less than 20');
    }
    else {
    alert ('The value you entered was not less than 20');
    }
    }



    now i dont think you should have to use the parsefloat function, i think .value returns it as a number... but I could be wrong. (javascript isn't my strongest language) If it looks like I am, pop it in there.

    Hope that helps?

  3. #3
    Member
    Join Date
    Aug 2010
    Location
    delhi
    Posts
    30

    answer to your question

    hi
    hope this will help
    just copy the code and paste it in notepad and save it with .htm extension and your work is done


    <HTML>
    <HEAD>
    <title> Compare A Number to 20 </title>
    <SCRIPT LANGUAGE="JavaScript">
    function doSomething () {
    if (document.getElementById('num1').value < 20 ){
    num2.value="less than 20"
    }
    else if(document.getElementById('num1').value != 20 ){
    num2.value="greater than 20"
    }
    else
    num2.value="equal to 20"
    }
    </SCRIPT>
    </HEAD>
    <BODY>

    <p>Enter a number</p>
    <p>
    <input type="text" id="num1" value= "" size="10" />
    <br/>
    <p>
    <input type="button" id="btn" value= "Compare With 20" onClick="javascript:doSomething()" />
    <br/>
    <p>
    <input type="text" id="num2" size="10" />
    </SCRIPT>
    </BODY>
    </HTML>


    read the code its very easy
    if you find this code confusing then sorry. post a message i will try to explain it

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. OddEven Function with Integer check in Javascript
    By Sunshine_Cat in forum Technical Support
    Replies: 1
    Last Post: July 15th, 2011, 06:34 PM
  2. JavaScript Function Calling From, Multiple Instances/Locations
    By apollobc in forum Webmastering and Programming
    Replies: 2
    Last Post: August 13th, 2008, 12:41 PM
  3. javascript function problem
    By csamuels in forum Webmastering and Programming
    Replies: 1
    Last Post: October 11th, 2006, 02:35 PM
  4. Javascript 'Imageswap' function fails...
    By Kurylo in forum General Tech Discussion
    Replies: 2
    Last Post: April 23rd, 2002, 05:12 PM
  5. Javascript 'Imageswap' function fails...
    By Kurylo in forum General Tech Discussion
    Replies: 5
    Last Post: April 22nd, 2002, 05:36 PM

Tags for this Thread

Posting Permissions

  • You may post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Recommended Sites: ResellerRatings Store Reviews