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

javascript onclick function in firefox

Status
Not open for further replies.

DannyTmoov2

IS-IT--Management
Jan 7, 2003
49
0
0
GB
Can someone tell me what's wrong in firefox's eyes with the below? When you hit the hyperlink nothing happens: -


<script language="JavaScript" type="text/JavaScript">
// enter button for login script
function submitenter(myfield,e)
{

var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
{
login.submit();
return false;
}
else
return true;
}
</script>

...


<A href="javascript:login.submit();">go</a>
 
Proper syntax:
Code:
<a href="#" onclick="document.forms['login'].submit(); return false;">go</a>

*cLFlaVA
----------------------------
[tt]insert funny quotation here.[/tt]
 
Something strange with the whole use of "e". The hyperlink doesn't show you using this function, but I assume you are somewhere.

The "test" you need to make is if the event object uses "keyCode" (IE) or "which" (FF):

Code:
<script type="text/javascript">
function submitenter(myfield,e)
{
   var keycode;

   if (navigator.appName == "Microsoft Internet Explorer")
   {
     keycode = e.keyCode;
   }
   else
   {
     keycode = e.which
   }

   if (window.event)
   {
     keycode = window.event.keyCode;
   }
   else
   if (e)
   {
     keycode = e.which;
   }

   if (keycode == 13)
   {
     login.submit();
     return false;
   }
   else
   {
     return true;
   }
}
</script>

And you have to call the function with the event:

<... onclick="submitenter(this,event)"; />

There is an article on my site that uses this code. The article is "Creating an HTML "ComboBox" control".



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Sorry, I had some of your code still in there:

Code:
<script type="text/javascript">
function submitenter(myfield,e)
{
   var keycode;

   if (navigator.appName == "Microsoft Internet Explorer")
   {
     keycode = e.keyCode;
   }
   else
   {
     keycode = e.which
   }

   if (keycode == 13)
   {
     return true;
   }
   else
   {
     return false;
   }
}
</script>

I left out the code for what to do if the keycode==13, but return false if you don't want the form to submit, return true if you do.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top