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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enter submits for, but I don't want it to...

Status
Not open for further replies.

grande

Programmer
Joined
Feb 14, 2005
Messages
657
Location
CA
I have the following function:
Code:
function keyclick(e) {
	var keynum;
	if (window.event) {    // IE
		keynum = e.keyCode; }
	else {                 // Opera, Netscape, Firefox
		keynum = e.which; }
	if (keynum == 13) {
		// Enter pressed, do other stuff
	}
}
[/quote]

That's supposed to trap the enter key.  However, the code never gets called because the page refreshes itself.  How do I tell the page not to refresh on Enter?

-------------------------
Call me barely Impressive Captain.
 
Okay, well it turns out that I just made a stupid mistake and forgot to Add the Attribute in my C# code. So now, the code gets fired, but the page still reloads.

-------------------------
Call me barely Impressive Captain.
 
Your code is not doing anything except analyzing whether the key pressed was the return key.

You need to detect the keypress event with:
Code:
onkeypress="return keyclick(event);"

And you need to add a return false to your function:
Code:
function keyclick(e) {
    var keynum;
    if (window.event) {    // IE
        keynum = e.keyCode; }
    else {                 // Opera, Netscape, Firefox
        keynum = e.which; }
    if (keynum == 13) {
        // Enter pressed, do other stuff
      [COLOR=blue]return false;[/color]
    }
}

It somewhat depends on how you are trapping the event.
If you put the onkeypress event in the submit button tag it will not stop enter from submitting unless focus is on the submit button. If you put it in the body tag it will trap all presses of enter on the page.


At my age I still learn something new every day, but I forget two others.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top