javascript click event/submit not working in Firefox

Posted on 2008-11-18
Last Modified: 2013-12-07
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');



      catch(er) {



      return false;


   else {

      return true;



Open in new window

Question by:chipotleit
    LVL 3

    Expert Comment

    Try this instead:

    characterCode  = (window.event) ? event.keyCode : e.keyCode;

    Author Comment

    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.
    LVL 3

    Accepted Solution

    What library are you using ( regarding the $ ), in a good library $('bla').click(); should just work.

    Author Comment

    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.

    Author Comment

    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.

    Author Closing Comment

    your comments got me thinking about this a little more, and started digging again.  I figured it had to be something small like this.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    #Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now