[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2166
  • Last Modified:

javascript click event/submit not working in Firefox

I have an aspx page with two different buttons in the same form.  Depending on what field the user is typing in, I want the enter key to submit using one button or the other.

asp button code: <asp:LinkButton ID="btnLogin" CssClass="button signIn" Text="Sign In" onClick="btnLogin_Click" runat="server" ValidationGroup="Login">

Using the following javascript function, I'm trying to get a button click event to fire.  The function seems to work in IE, Safari, etc., but not Firefox.
buttonLogin: function(e) {
   if(window.event) // IE
   {
      characterCode = e.keyCode;
   }
   else if(e.which) // Netscape/Firefox/Opera
   {
      characterCode = e.which;
   }
   
   if(characterCode == 13) {
      try {
         var evt = document.createEvent("MouseEvents");
         evt.initMouseEvent("click", true, true, window,
            0, 0, 0, 0, 0, false, false, false, false, 0, null);
         var btn = $('btnLogin');
         btn.dispatchEvent(evt);
      }
      catch(er) {
         $('btnLogin').click();
      }
      return false;
   }
   else {
      return true;
   }
}

Open in new window

0
chipotleit
Asked:
chipotleit
  • 4
  • 2
1 Solution
 
wassa_rCommented:
Try this instead:

characterCode  = (window.event) ? event.keyCode : e.keyCode;
0
 
chipotleitAuthor Commented:
I'll need to add some clarification.  In Firefox, it gets the keypress and will get into the "if(characterCode == 13)" statement, but won't actually fire the click event on either route.
0
 
wassa_rCommented:
What library are you using ( regarding the $ ), in a good library $('bla').click(); should just work.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
chipotleitAuthor Commented:
The site is using mootoolsCore 1.2

If I just use the $('btnLogin').click(); it won't do anything in Firefox.

Firefox currently goes into the try block of code, but the code doesn't produce any actions or errors, so won't go into the catch block either.
0
 
chipotleitAuthor Commented:
The Answer:

the asp LinkButton is rendered in html as an anchor "a" tag, which doesn't seem to support a click event in Firefox.  I changed the LinkButton to an image button, so it is rendered as an "input" and works fine now.
0
 
chipotleitAuthor Commented:
your comments got me thinking about this a little more, and started digging again.  I figured it had to be something small like this.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now