...

View Full Version : Making text box connected to Button



Jolly Rancher
08-25-2008, 06:03 PM
Hi, this is my first time doing Javascript and I tried to create a small word game but it didn't come out right. In the example, the word "happy" is suppose to be the correct word and if the person guesses it correctly an alert box would pop up telling that person that they got it correct. But when I test it out nothing pops up. I think what's happening is that the text box is not really connected to the "Enter" button so the button does not work accordingly. So is there any way to fix this? Here is the code...



<html>
<head>
<script type="text/javascript">
function unscramb()
{
var x = document.forms['form1'].happy.Enter;
x="happy";
if (x==happy);
{
alert("Great! You got it!");
}
else
{
alert("Sorry, try again!");
}
}
</script>
</head>
<body>
<form name="form1">
Unscramble the word. <input type="text" size=5 maxlength=5 name="happy">
<input type="button" onclick="unscramb()"
value="Enter">
</form>
</body>
</html>


Thanks for any help!

Philip M
08-25-2008, 07:44 PM
<html>
<head>
<script type="text/javascript">
function unscramb() {
var x = document.form1.happy.value;
if (x.toLowerCase() == "happy") { // make case insensitive
alert("Great! You got it!");
}
else {
alert("Sorry, try again!");
document.form1.happy.value = ""; // clear the box
document.form1.happy.focus(); // and focus on it again
}
}
</script>
</head>
<body>
<form name="form1">
Unscramble the word. <input type="text" size=5 maxlength=5 name="happy">
<input type="button" onclick="unscramb()" value="Enter">
</form>
</body>
</html>


A child of five would understand this. Send someone to fetch a child of five.
Groucho Marx

Jodarecode
08-25-2008, 07:50 PM
Try this!


<html>
<head>
<script type="text/javascript">

function unscramb() {

x = document.form1.happy.value

if (x == "happy") {

alert("Great! You got it!")

}
else if(x != "happy") {

alert("Sorry, try again!")

}
}
</script>
</head>
<body>
<form name="form1">
Unscramble the word. <input type="text" size=5 maxlength=5 name="happy">
<input type="button" onclick="unscramb()"
value="Enter">
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum