Jump to content

Need help! Eventhandler for Enter key.


MetaCry

Recommended Posts

Hey guys,

So... I have this textfield with a button next to it.

If you click the button you trigger a function named "getaGame()".

But, I want to be able to trigger the function with just hitting Enter while being in the textfield.

 

Here is what I have done:

<script language="javascript">

function getaGameTextbox() {

if(window.event.keyCode == 13)

getaGame();

}

</script>

 

Here is the textfield and button:

 

<div id="form">

<form name="link" onsubmit="getaGame()">

 

<form method="link" >

<input type="text" size="45" onKeyUp="getaGameTextbox()" />

<input type="button" name="submit" value="submit" onClick="getaGame()" />

</form>

</div>

 

But I can't get it to execute the function when Enter is pressed :(

Any suggestions?

Link to comment
Share on other sites

Problem has been solved.

 


<!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>
<script language="javascript">
function getaGameTextbox() {
if(window.event.keyCode == 13) {
     getaGame();
     return false;
}
}

function get_random()
{
var ranNum= Math.floor(Math.random()*10);
return ranNum;
}

function getaGame()
{
var whichGame=get_random();

var game=new Array(10)
game[0]= "answers/1.html";
game[1]= "answers/2.html";
game[2]= "answers/3.html";
game[3]= "answers/4.html";
game[4]= "answers/5.html";
game[5]= "answers/6.html";
game[6]= "answers/7.html";
game[7]= "answers/8.html";
game[8]= "answers/9.html";
game[9]= "answers/10.html";

location.href = game[whichGame];

}
</script>
<link rel="stylesheet" type="text/css" href="css/main.css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Found out who stole your crap!</title>
</head>
<body>
<div id="wrapper">
<div id="border">
</div>
<div id="header">
 <img src="images/header.jpg" width="1000" height="200" alt="image" />
 </div>

<p id="text">
WHAT CRAP WAS STOLEN?
</p>
<div id="form">
<form name="link" onsubmit="getaGame()">

<form method="link" >
<input type="text" size="45" onKeyPress="return getaGameTextbox()"/>
<input type="button" name="submit" value="submit" onClick="getaGame()" />
</form>
</div>
</div>
</div>
</body>
</html>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...