Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to control Enter from within Input field?

Status
Not open for further replies.

requested89

IS-IT--Management
Sep 6, 2005
44
GB
How can I control what happens when a user presses Enter within a Text field?

I want it so that the form is NOT submitted but instead calls a javascript fucntion.

Thanks in advance,

Chris
 
You'd add an onclick event to the button:
Code:
<input type="button" name="link" onclick="btnClicked()">
And then the following javascript:
Code:
<script type="text/javascript">
function btnClicked() {
  alert('Boo!');
}
</script>
Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
I have no idea if I was even thinking of your post when I put that solution together [smile]. Let's try again...

How can I control what happens when a user presses Enter within a Text field?
You need to add a new tag to the form:
Code:
<form name="myForm" action="" onsubmit="return myFunction();">
...
</form>
That way the function will be run every time the form is submitted (regardless of whether you use a button or if you press enter).

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
Furthermore...

The onsubmit in my previous post is set up to return whatever was returned from your function. If you return false, then the onsubmit will return false... preventing the form from being submitted. If your function returns true, then the form will recieve true from the onsubmit and the form will be submitted.

Hope this sorts your problem out.

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]

What is Javascript? faq216-6094
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top