By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

Help with javascript (again)

P: n/a
I'm not that great at reading javascripts.

Instead of doing nothing I'd like to return a Tab-character, if Enter
occurs:

<script language="javascript" type="text/javascript" >
//disable enter-key
function kH(e) {
var pK = e ? e.which : window.event.keyCode;
return pK != 13;
}
document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);
</script>

To help me undertand: What is "e"? What is "!=" ? (i'm used to vb).

Regards /Snedker
Dec 5 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi Morten,
Instead of doing nothing I'd like to return a Tab-character, if Enter
occurs:

<script language="javascript" type="text/javascript" >
//disable enter-key
function kH(e) {
var pK = e ? e.which : window.event.keyCode;
return pK != 13;
}
document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);
</script>
you want to retrun a Tab, but to whom? This example script will disable the
enter key simply be looking for the keyCode (which is something like the
ASCII code).

Maybe you should ask what you do instead?!

--
Best regards | Schöne Grüße
Michael

Microsoft MVP - Most Valuable Professional
Microsoft MCAD - Certified Application Developer

http://weblogs.asp.net/mschwarz/
http://www.ajaxpro.info/
Dec 5 '06 #2

P: n/a
On Tue, 5 Dec 2006 15:07:54 +0100, "Michael Schwarz"
<no****@schwarz-interactive.dewrote:

I wish to move to the next field in the tab-index. I reckon it is
something like nextfield.focus - but still, I don't know where to put
it.

I guess what I'm really missing is a Javascript Beginners Guide. :-)

But till then? ;-)
>Instead of doing nothing I'd like to return a Tab-character, if Enter
occurs:

<script language="javascript" type="text/javascript" >
//disable enter-key
function kH(e) {
var pK = e ? e.which : window.event.keyCode;
return pK != 13;
}
document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);
</script>

you want to retrun a Tab, but to whom? This example script will disable the
enter key simply be looking for the keyCode (which is something like the
ASCII code).

Maybe you should ask what you do instead?!
Dec 5 '06 #3

P: n/a
Morten,

Good morning!

To explain the ?, look at:
http://www.c-point.com/javascript_tu...onditional.htm

Basically the ? is an operator that does a quick if then else for you.
so the line
var pK = e ? e.which : window.event.keyCode

is saying that if e is true, then set pK = e.which, which means that
the user is using Netscape/Firefox and if e is false, set pK to
window.event.keyCode , which means that the user is using IE.

This is another way of doing the same:

if(window.event) // IE
{
pK = e.keyCode
}
else if(e.which) // Netscape/Firefox/Opera
{
pK= e.which
}

!= means not equal to

Hope this is of some help,
Kathryn

Morten Snedker wrote:
On Tue, 5 Dec 2006 15:07:54 +0100, "Michael Schwarz"
<no****@schwarz-interactive.dewrote:

I wish to move to the next field in the tab-index. I reckon it is
something like nextfield.focus - but still, I don't know where to put
it.

I guess what I'm really missing is a Javascript Beginners Guide. :-)

But till then? ;-)
Instead of doing nothing I'd like to return a Tab-character, if Enter
occurs:

<script language="javascript" type="text/javascript" >
//disable enter-key
function kH(e) {
var pK = e ? e.which : window.event.keyCode;
return pK != 13;
}
document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);
</script>
you want to retrun a Tab, but to whom? This example script will disable the
enter key simply be looking for the keyCode (which is something like the
ASCII code).

Maybe you should ask what you do instead?!
Dec 5 '06 #4

P: n/a
Hi,

kbutterly wrote:
Morten,

Good morning!

To explain the ?, look at:
http://www.c-point.com/javascript_tu...onditional.htm

Basically the ? is an operator that does a quick if then else for you.
so the line
var pK = e ? e.which : window.event.keyCode

is saying that if e is true, then set pK = e.which, which means that
Actually, this works because for JavaScript, if an object is null, it
can be tested against false. If it is not null, it can be tested against
true. So the line above actually means "if e is not null, assign e.which
to pK, or else assign window.event.keyCode. This is to avoid a null
reference exception, should e be null.

Note that the code above is not 100% safe, because window.event doesn't
get tested against null. Additionally, maybe e.which or
window.event.keyCode are undefined.

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Dec 5 '06 #5

P: n/a
Hi Morten,

I wish to move to the next field in the tab-index. I reckon it is
something like nextfield.focus - but still, I don't know where to put
it.
the only thing you can do is to call the .focus() method of the next
control:

<input type="text" id="field1"
onkeypress="if(window.event.keyCode==13)document.g etElementById('field2').focus();">

<input type="text" id="field2">
I guess what I'm really missing is a Javascript Beginners Guide. :-)
;)
--
Best regards | Schöne Grüße
Michael

Microsoft MVP - Most Valuable Professional
Microsoft MCAD - Certified Application Developer

http://weblogs.asp.net/mschwarz/
http://www.ajaxpro.info/
Dec 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.