Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Detecting if Enter Key Pressed on Form
Old 08-10-2006, 10:42 PM Detecting if Enter Key Pressed on Form
Galaxian's Avatar
Dingleberry!

Posts: 825
Name: Rich
Location: United Kingdom
Trades: 0
I'm an absolute javascript newbie so please forgive me.

I need a script that detects when the enter key has been pressed on an input box called "message" then deletes the contents of it about half a second later and refocuses on it.

I have:

Code:
function checkenter() {
if (document.chat.message.event.keycode == 13){
setTimeout("document.chat.message.value = ''",500);
document.chat.message.focus();
}
}
and

Code:
<input name="message" type="text" id="message" style="border: #000000 solid 1px; background-color:#FEEBA7" size="50" onkeydown="checkenter();" />
But it doesn't seem to work.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
 
Register now for full access!
Old 08-10-2006, 11:28 PM Re: Detecting if Enter Key Pressed on Form
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
try using "window.event.keyCode" or pass the event variable to the function:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

	<head>
		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
		<title>Untitled Page</title>
		<script type="text/javascript"><!--
function checkForm(e) {
	if (e.keyCode == 13) {
		alert("You pressed enter - yay!");	
	}
}
//-->
</script>
	</head>

	<body bgcolor="#ffffff">
		<input type="text" onkeydown="checkForm(event); return false;">
	</body>

</html>
Here is a good page on how different browsers hand the event object: Javascript - Event accessing

Last edited by funkdaddu; 08-10-2006 at 11:33 PM..
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 08-11-2006, 03:55 AM Re: Detecting if Enter Key Pressed on Form
Galaxian's Avatar
Dingleberry!

Posts: 825
Name: Rich
Location: United Kingdom
Trades: 0
I just tried it and I was unable to type anything in the text box
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 08-11-2006, 10:33 AM Re: Detecting if Enter Key Pressed on Form
funkdaddu's Avatar
Web Design Snob

Posts: 635
Trades: 0
Whoops, take out the "return false;"
funkdaddu is offline
Reply With Quote
View Public Profile Visit funkdaddu's homepage!
 
Old 08-11-2006, 07:27 PM Re: Detecting if Enter Key Pressed on Form
Galaxian's Avatar
Dingleberry!

Posts: 825
Name: Rich
Location: United Kingdom
Trades: 0
ok that is great thank you very much! It deletes it But doesnt refocus ..uh


function checkForm(e) {
if (e.keyCode == 13) {
setTimeout("document.chat.message.value = ''",500);
document.chat.message.focus();
}
}
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Old 08-11-2006, 09:41 PM Re: Detecting if Enter Key Pressed on Form
Galaxian's Avatar
Dingleberry!

Posts: 825
Name: Rich
Location: United Kingdom
Trades: 0
Fixed. I did a settimeout on the focus.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Galaxian is offline
Reply With Quote
View Public Profile Visit Galaxian's homepage!
 
Reply     « Reply to Detecting if Enter Key Pressed on Form
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.16833 seconds with 12 queries