-
March 30th, 2010, 09:36 PM #1Junior 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>
-
March 30th, 2010, 10:50 PM #2Member
- 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?
-
August 8th, 2010, 10:29 AM #3Member
- 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
-
OddEven Function with Integer check in Javascript
By Sunshine_Cat in forum Technical SupportReplies: 1Last Post: July 15th, 2011, 06:34 PM -
JavaScript Function Calling From, Multiple Instances/Locations
By apollobc in forum Webmastering and ProgrammingReplies: 2Last Post: August 13th, 2008, 12:41 PM -
javascript function problem
By csamuels in forum Webmastering and ProgrammingReplies: 1Last Post: October 11th, 2006, 02:35 PM -
Javascript 'Imageswap' function fails...
By Kurylo in forum General Tech DiscussionReplies: 2Last Post: April 23rd, 2002, 05:12 PM -
Javascript 'Imageswap' function fails...
By Kurylo in forum General Tech DiscussionReplies: 5Last Post: April 22nd, 2002, 05:36 PM



LinkBack URL
About LinkBacks



Reply With Quote

next we will see stories of peoples lights being hacked.
Is It Just Me? v233893843