onclick event in anchor tag not working in IE

44,594

Solution 1

Can you provide a bit more context? Such as where and how the submit function is defined? With just the above, it should work -- except:

You probably also want to return false; in there though, to cancel the default action. E.g.:

<a href="javascript:void(0);" onclick="submit();return false;">Sign In</a>

It may be that the default action is happening immediately after the submit and interfering with it.

Edit: Just for fits and giggles, try using a different name for your submit function. IE has namespacing issues. If you have anything with the id or name attribute set to "submit", for instance, that could be an issue...

Solution 2

For others who have come across this problem more recently, for me it was due to IE 11 using compatibility mode on the site. It saw I was on the same domain, assumed the site was an intranet site and automatically switched to compatibility mode which broke some of the Javascript (and caused some other unexpected display issues).

You can turn off this 'feature' by clicking on the settings cog and choosing "Compatibility View Settings". There is a tick box that was set called "Display intranet sites in Compatibility View".

Share:
44,594

Related videos on Youtube

Mark
Author by

Mark

Updated on July 09, 2022

Comments

  • Mark
    Mark almost 2 years

    The following works fine in Firefox and Chrome, but IE 8 will not call the submit() method when the anchor link is clicked.

    <a href="javascript:void(0);" onclick="submit();">Sign In</a>
    

    The submit method is defined on the same page as follows:

    <head>
    <script type="text/javascript">
    
    function submit()
    {
    // other code                                              
    document.forms[0].submit();
    }  
    
    </script>
    </head>
    
    • Umair A.
      Umair A. almost 14 years
      did you try onclick="javascript:submit();" ?
    • T.J. Crowder
      T.J. Crowder almost 14 years
      @Umair: There's no need for the javascript: prefix on onclick or similar event handler attributes. This isn't the href.
    • Umair A.
      Umair A. almost 14 years
      once it had worked for me so I guess it is
    • T.J. Crowder
      T.J. Crowder almost 14 years
      @Umair: With respect, it must have been something else. In that location, javascript: is a label (and one you're not using), not a protocol. In the href, it would be a protocol.
    • Trick
      Trick almost 14 years
      maybe stupid question: is there a form? :) or are there more than one?
    • irishbuzz
      irishbuzz almost 14 years
      If you change the submit() function to function submit() { alert('it works'); } What happens?
  • Mark
    Mark almost 14 years
    I tried adding "return false;" with no effect. The IE debugger shows the call to the onclick code, but won't enter the submit() method.
  • T.J. Crowder
    T.J. Crowder almost 14 years
    @Mark: I'm not sure I know what you mean by the debugger showing it entering the onclick code but not entering the submit method. Is there an exception thrown? If not, what happens such that it "won't" enter the function?
  • Mark
    Mark almost 14 years
    Thanks for responding T.J. It's odd really. I set the IE 8 debugger to "break on all". As soon as I click the "Sign In" link, the debugger breaks and highlights onclick="submit();" in the anchor. I assume the highlighting is to indicate the next instruction to be executed. To be safe, I also set a breakpoint inside the submit method. From this point I click "Step Into" or "Step Over" on the debugger menu. Both have the same effect of highlighting the void(0) method. For some reason IE will not execute the submit() function. I don't see any indication of an error.
  • Mark
    Mark almost 14 years
    You were right T.J. It was a name space conflict. IE uses submit for something. Once I changed the function name it worked. Thanks again for the help.
  • T.J. Crowder
    T.J. Crowder almost 14 years
    @Mark: Excellent! Glad that helped, I should have thought of it to start with.
  • Sachin Sharma
    Sachin Sharma over 11 years
    @T.J.Crowder I faced the same namespace problem with <a onclick="click()"><\a> function. Once I changed the function name things worked. Thanks for this conversation.
  • Abdul Rehman Sayed
    Abdul Rehman Sayed about 6 years
    This did not work for me in ie9. I had to shift to jquery