Solved

How to call javascript when users press Enter

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…

930 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