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!

Javascript/ Firefox onKeyPress event error

Status
Not open for further replies.

keoflex

Programmer
Jul 28, 2009
3
US
I am having an error with firefox not wanting to accept an on key up event. I know that you have to program things differently for IE and FF but am having trouble doing so. Here is my working code in IE.

there are several text fields that run this same function and some text fields that will not fun the function.

Thanks in advance for any help.

HTML

<input name="Lname" type="text" class="input" id="nameinput" tabindex="2" value="" onKeyDown="KeyCheck(form1)" />

Javascript
function KeyCheck(frm)
{
var KeyID = event.keyCode;
//alert(event.keyCode);
switch(KeyID)
{

case 13:
//document.Form1.cusLookup.currentValueIndices =1;
frm.cusLookup.focus();
frm.cusLookup.options[1].selected = "1";
//document.Form1.L.value = "Ctrl";
//
}
}

Javascript2 (this is what i am attempting because the above does not work in firefox)

function KeyCheck(e)
{
var KeyID = (window.event) ? event.keyCode : e.keyCode;
switch(KeyID)
{
case 13:
//document.Form1.cusLookup.currentValueIndices =1;
frm.cusLookup.focus();
frm.cusLookup.options[1].selected = "1";
//document.Form1.L.value = "Ctrl";
//
}
}
 
I am having an error with firefox not wanting to accept an on key up event.

Perhaps that's because your HTML assigns code to the onKeyDown handler instead of the onKeyUp handler?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
I appologies i meant the o key down event does not work either way the javascript throws an error when it is accessed
 
Where is 'form1' defined? You're passing it through as the parameter, but I don't see anywhere it's defined.

Perhaps try passing 'this.form' instead (without the quotes).

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
<FORM
ACTION="../dbcon/dbcon.php"
METHOD="POST"
NAME="form1"
onSubmit="return validate(form1)">


i only posted the relevent code. because of the way firefox handles on key events this code will never work for firefox but the code at the bottom will i just dont know how to reference it

function KeyCheck(frm)
{
var KeyID = event.keyCode;
//alert(event.keyCode);
switch(KeyID)
{

case 13:
//document.Form1.cusLookup.currentValueIndices =1;
frm.cusLookup.focus();
frm.cusLookup.options[1].selected = "1";
//document.Form1.L.value = "Ctrl";
//
}
}


this code will work
function KeyCheck(e)
{
var KeyID = (window.event) ? event.keyCode : e.keyCode;
switch(KeyID)
{
case 13:
//document.Form1.cusLookup.currentValueIndices =1;
frm.cusLookup.focus();
frm.cusLookup.options[1].selected = "1";
//document.Form1.L.value = "Ctrl";
//
}
}


 
Just because your form has a name of "form1" doesn't mean you can simply refer to "form1" and expect it to work (it might in IE, but that's because IE allows sloppy and non-standard coding practices).

I suggest you take my previous advice and use "this.form", or, if "form1" isn't the form for the input in question, use "document.forms['form1']".

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Why the difference in args?

Code:
function KeyCheck(frm)
{

...

function KeyCheck(e)
{

"e" is an event object given by Firefox right? But your onKeyDown always sends "form1" as the param... so in the second case, "e" = "form1" and not the event?

I made a simple text-based RPG in JavaScript by using the key event handlers and it handles both Firefox and IE, look at the source code here: to see how I do it; it might help you with your code.

Cuvou.com | My personal homepage
Code:
perl -e '$|=$i=1;print" oo\n<|>\n_|_";x:sleep$|;print"\b",$i++%2?"/":"_";goto x;'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top