Solved

How to call javascript when users press Enter

Posted on 2007-11-16
3
6,581 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now