Solved

How to call javascript when users press Enter

Posted on 2007-11-16
3
6,584 Views
Last Modified: 2008-02-01
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
Comment
Question by:fippif
  • 2
3 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20302932
0
 
LVL 17

Expert Comment

by:gops1
ID: 20302969
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
 
LVL 17

Accepted Solution

by:
gops1 earned 125 total points
ID: 20303947
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question