Random Posted February 9, 2007 Share Posted February 9, 2007 I'm fairly new to javascript and I have been having trouble learning it. I need to finish a project where I have to be able to type in a number and depending what that number is a message will appear on the screen. Like if I type a number between 1 and 10 message A will appear, 11 to 20: Message B. I have the if else statement set up, I'm just having trouble displying it. If anyone knows off hand how to do it or has an example or a direct link on how to do this I'd really appreciate it. I've searched all over the web and cant seem to find it. Thanks. Link to comment Share on other sites More sharing options...
aspnetguy Posted February 9, 2007 Share Posted February 9, 2007 <html><head> <title></title> <script> var num = prompt("Enter a number",""); showMessage(num); function showMessage(num) { if(!isNaN(num)) { if(num < 1) alert("The number needs to be greater than 0"); else if(num > 0 && num < 11) alert("1 to 10"); else if(num > 10 && num < 21) alert("11 to 20"); else alert("Greater than 20"); } else alert("You need to enter a number!"); } </script></head><body></body></html> Link to comment Share on other sites More sharing options...
Random Posted February 9, 2007 Author Share Posted February 9, 2007 Thanks a lot that really helped, but I want to stay away from prompts and alerts. What I need is to have the user enter th number in a text box on the page than display it, I'm just having problems getting that to work Link to comment Share on other sites More sharing options...
aspnetguy Posted February 9, 2007 Share Posted February 9, 2007 Now that I finished your homework for you...did you learn something? <html><head> <title></title> <script> function showMessage() { var num = parseInt(document.getElementById("num").value); var msg = ""; if(!isNaN(num)) { if(num < 1) msg = "The number needs to be greater than 0"; else if(num > 0 && num < 11) msg = "1 to 10"; else if(num > 10 && num < 21) msg = "11 to 20"; else msg = "Greater than 20"; } else msg = "You need to enter a number!"; document.getElementById("message").value = msg; } </script></head><body> <input id="num"> <input type="button" value="Send" onclick="showMessage()"> <br><br> Message:<br> <textarea cols="30" rows="2" id="message"></textarea></body></html> Link to comment Share on other sites More sharing options...
Random Posted February 9, 2007 Author Share Posted February 9, 2007 Yes I did and thank you so much, I was really stuck and this help me finish, I really appreciate it. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now