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

How it works :-? 1

Status
Not open for further replies.

flac0

Programmer
Oct 31, 2001
22
UY
Hi!
I don´t understand how works the form, when a user press Enter key instead a submit button. I got a lot of pages with similar source but some of them works diferent when user press Enter. In some cases the Enter works like submit button, but in some cases have a confuse behavior.

How it works? How or where must define the Enter behavior. How can i do to set the Enter Press event to work like a default submit button?

Thanks in advance guys, Flac0 >;)

My keyboard are on-fire baby! ;)
 
Typically if you are not handling the submit action with a
javascript function, the enter key by default activates
the submit button on the form.

If you are going through some sort of validation using
javascript after the submit button is clicked then the
script handles the submit for you.

SomeOne else will have to help with the ? about making the
enter key act as the submit button....

2b||!2b
 
From my experience (with Internet Explorer), if focus is on a text box (&quot;<input type=text...>&quot;) and the box is between <FORM> tags, then hitting enter will cause submission.

If you want to have submission when focus is elsewhere, add an onKeyDown=checkEnter() to and tag which might have focus (even the BODY tag) and include this function in your script:

Code:
function checkEnter(field, evt)
{
 if(event.keyCode == 13)	//enter
  form1.submit();
}//end checkEnter(var, var)

*assuming the name of your form is 'form1'.

'hope this helps.

--Dave
 
It seems like i must to handle the event...it´s ok, no problem. I can live with that ;)

Thank you guys...it was very useful >;)

----------------------------------------
Other world it´s possible. Keep on fighting! >;)
----------------------------------------
 
Dave that was a very interesting response...

Where can a list of the keycodes be obtained at?
so far I have 13 is enter..
That is very useful information.

Thanks,
Brian

P.S. (*)
 
Thanks!

Honestly, I'm not really sure where to find a comprehensive list. What I usually do is write a function like checkEnter(...) to trigger on a onKeyDown event and (temporarily) put an alert in it like so:

Code:
function checkEnter(field, evt)
{
 alert(event.keyCode);
}

...and then I just press the keys in which I'm interested. It gets a little confusing when you bring shift, alt, and cntl keys into it.

It should be noted that according to O'Reilly's Javascript: The Definitive Guide, the keyCode is a IE property only. Netscape allows use of event.which which returns a code. I imagine they're the same codes in both cases, but I don't use Netscape and have never tested that out.

Thanks again!

--Dave

P.S., I highly recommend the O'Reilly book for quick reference!
 
Thanks Dave,

I have the O'reilly book and also have the Beginning JavaScript Book published by Wrox. (The Wrox book is my favorite because it's efforts work hard at addressing cross-browser compatability) It even takes you server side via .asp.

I just never really paid attention to this subject, and for some reason your post triggered it for me. I really appreciate your explanation, it is going to be quite helpful in the future.

I think I am going to go back and re-read that section.

Also your quite welcome for the (*). Sometimes I don't think people get credit for assisting others in that fashion and lately I have been trying to give credit where credit is due. (Even when I didn't submit the original post)

On that note I would just like to add a word of thanks to all the JavaScript Guru's that contribute to making this site so amazing.


Once again thanks,
Brian
 
A place to get a table with keycodes:

THE ASCII Character TABLE

www.asciitable.com

I hope help you.....





 
I got a little web page with a script on it to show keycodes from one of these forums I use a lot. Sorry I can't remember who posted it. To find the keycode just type the letter ect. in the text box.
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd">[/URL]
<html>
<head>
<title>test bed</title>
<script language="JavaScript">
function checkKeycode(e) {
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    alert("keycode: [" + keycode + "]");
}
</script>
</head>
<body>
<p>Type in here:
<input type="text" size="10" value="" onKeyPress="checkKeycode(event)">
</p>
</body>
</html>
Hope it's usefull.

Glen
 
Regarding the function GlenMac posted on 3/15/04 which displays the character code for a given character: I've been unsuccessful modifying the function to return the character codes for keys such as PgUp/Dn, the arrows, function keys, etc.

I know these keys return a 2-byte sting and the first character is character 0.

Does anyone have any ideas?

Many thanks in advance.
 
Sam,

You're not looking to change the function, but rather the event that triggers it. Change 'onKeyPress' to 'onKeyDown'. That works in IE.

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top