• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6596
  • Last Modified:

How to call javascript when users press Enter

I am using an input type "image" for which "onclick=quotego()" (quotego() is my script). It works fine when users click on the image. But I want to be able to call  my script when users press Enter.

Thanks.

<form>
							<td><table width="180" border="0" cellpadding="0" cellspacing="0" bgcolor="#898989">
							<tr><td width="138" height=38 align=left valign=top class="F_TD_L12"><input type=text class="form3" id=QuoteTicker onclick=clearText("QuoteTicker") value="<Enter ticker for quote>"></td>
							<td width=42 align=left valign=top class="F_TD_L0"><input type=image src=/images/ico_go.gif onclick=quotego()></td>
							</tr>
							</form>

Open in new window

0
fippif
Asked:
fippif
  • 2
1 Solution
 
nizsmoDeveloperCommented:
0
 
gops1Commented:
Have a look at this sample code:
<html>
	<head>
		<title>Script Demo Gops</title>
		<script type="text/javascript">
			function captureEvt(evt){
				evt = (evt) ? evt : event;
				var charCode = (evt.which) ? evt.which : evt.keyCode;
				if(charCode==13) alert("Enter key pressed");
			}
		</script>
	</head>
<body>
	<form name="calcs">
		Press Enter<input type="text" value="" name="emp" onkeypress="captureEvt(event)"><br>
		Press Enter<input type="text" value="" name="val" onkeypress="captureEvt(event)"><br>
	</form>
</body>
</html>

Open in new window

0
 
gops1Commented:
Input type image is like submit button. In this case do it this way:

<html>
	<head>
		<title>Script Demo Gops</title>
		<script type="text/javascript">
			function quotego(){
				alert("Here");
			}
		</script>
	</head>
<body>
<form onsubmit="return quotego()">
	<table width="180" border="0" cellpadding="0" cellspacing="0" bgcolor="#898989">
		<tr>
			<td width="138" height=38 align=left valign=top class="F_TD_L12"><input type=text class="form3" id=QuoteTicker onclick=clearText("QuoteTicker") value="<Enter ticker for quote>"></td>
			<td width=42 align=left valign=top class="F_TD_L0"><input type=image src="/images/ico_go.gif"></td>
		</tr>
	</table>
</form>
</body>
</html>

Open in new window

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now