Solved

How to call javascript when users press Enter

Posted on 2007-11-16
3
6,587 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

733 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