Help with javascript (again)

M

Morten Snedker

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
 
M

Michael Schwarz

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/
 
M

Morten Snedker

On Tue, 5 Dec 2006 15:07:54 +0100, "Michael Schwarz"

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? ;-)
 
K

kbutterly

Morten,

Good morning!

To explain the ?, look at:
http://www.c-point.com/javascript_tutorial/jsoprconditional.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
 
L

Laurent Bugnion

Hi,
Morten,

Good morning!

To explain the ?, look at:
http://www.c-point.com/javascript_tutorial/jsoprconditional.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
 
M

Michael Schwarz

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.getElementById('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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top