<p>The .Net generated code for a form with the "DefaultButton" attribute set contains poor javascript that allows the functionality to work in IE but not in other browsers (Firefox specifcially). </p>

<p>Hitting enter key does submit the form with all browsers but Firefox cannot disregard the key press when it happens inside of a &lt;textarea&gt; control. The result is a multiline text area control that cannot be multiline in Firefox as the enter key submits the form instead of creating a new line. </p>

<p>For more information on the bug, <a href="http://www.velocityreviews.com/forums/t367383-formdefaultbutton-behaves-incorrectly.html" rel="noreferrer">read it here</a>.</p>

<p>This could be fixed in Asp.Net 3.0+ but a workaround still has to be created for 2.0. </p>

<p>Any ideas for the lightest workaround (a hack that doesn't look like a hack =D)? The solution in the above link scares me a little as it could easily have unintended side-effects.</p>

[{'answer_id': 50039, 'author': 'Espo', 'author_id': 2257, 'author_profile': 'https://Stackoverflow.com/users/2257', 'pm_score': 1, 'selected': False, 'text': '<p>For this particular issue, the reason is because javascript generated by \nASP.NET 2.0 has some IE only notation: event.srcElement is not availabe in \nFireFox (use event.target instead):</p>\n\n<pre><code>function WebForm_FireDefaultButton(event, target) {\nif (!__defaultFired &amp;&amp; event.keyCode == 13 &amp;&amp; !(event.srcElement &amp;&amp; \n(event.srcElement.tagName.toLowerCase() == "textarea"))) {\nvar defaultButton;\nif (__nonMSDOMBrowser) {\ndefaultButton = document.getElementById(target);\n}\nelse {\ndefaultButton = document.all[target];\n}\nif (defaultButton &amp;&amp; typeof(defaultButton.click) != \n"undefined") {\n__defaultFired = true;\ndefaultButton.click();\nevent.cancelBubble = true;\nif (event.stopPropagation) event.stopPropagation();\nreturn false;\n}\n}\nreturn true;\n}\n</code></pre>\n\n<p>If we change the first 2 lines into:</p>\n\n<pre><code>function WebForm_FireDefaultButton(event, target) {\nvar element = event.target || event.srcElement;\nif (!__defaultFired &amp;&amp; event.keyCode == 13 &amp;&amp; !(element &amp;&amp; \n(element.tagName.toLowerCase() == "textarea"))) {\n</code></pre>\n\n<p>Put the changed code in a file and then do</p>\n\n<pre><code>protected void Page_Load(object sender, EventArgs e)\n{\nClientScript.RegisterClientScriptInclude("js1", "JScript.js");\n}\n</code></pre>\n\n<p>Then it will work for both IE and FireFox.</p>\n\n<p>Source:</p>\n\n<p><a href="http://www.velocityreviews.com/forums/t367383-formdefaultbutton-behaves-incorrectly.html" rel="nofollow noreferrer">http://www.velocityreviews.com/forums/t367383-formdefaultbutton-behaves-incorrectly.html</a></p>\n'}, {'answer_id': 50063, 'author': 'harpo', 'author_id': 4525, 'author_profile': 'https://Stackoverflow.com/users/4525', 'pm_score': 3, 'selected': True, 'text': '<p>I use this function adapted from codesta.  [Edit: the very same one, I see, that scares you!  Oops.  Can\'t help you then.]</p>\n\n<p><a href="http://blog.codesta.com/codesta_weblog/2007/12/net-gotchas---p.html" rel="nofollow noreferrer">http://blog.codesta.com/codesta_weblog/2007/12/net-gotchas---p.html</a>.</p>\n\n<p>You use it by surrounding your code with a div like so.  You could subclass the Form to include this automatically.  I don\'t use it that much, so I didn\'t.</p>\n\n<pre>\n&lt;div onkeypress="return FireDefaultButton(event, \'&lt;%= aspButtonID.ClientID %&gt;\')"&gt;\n    (your form goes here)\n&lt;/div&gt;\n</pre>\n\n<p>Here\'s the function.</p>\n\n<pre>\nfunction FireDefaultButton(event, target) \n{\n    // srcElement is for IE\n    var element = event.target || event.srcElement;\n\n    if (13 == event.keyCode && !(element && "textarea" == element.tagName.toLowerCase())) \n    {\n        var defaultButton;\n        defaultButton = document.getElementById(target);\n\n        if (defaultButton && "undefined" != typeof defaultButton.click) \n        {\n            defaultButton.click();\n            event.cancelBubble = true;\n            if (event.stopPropagation) \n                event.stopPropagation();\n            return false;\n        }\n    }\n    return true;\n}\n</pre>\n'}, {'answer_id': 1454033, 'author': 'Jan Aagaard', 'author_id': 37147, 'author_profile': 'https://Stackoverflow.com/users/37147', 'pm_score': 2, 'selected': False, 'text': '<p>It seems that the fix codesta.com that harpo link to is no longer necessary, since the fix event.srcElement is not integrade in ASP.NET 3.5. The implementation of DefaultButton does however still have some problems, because it is catching the Enter key press in too many cases. For example: If you have activated a button in the form using tab, pressing Enter should click on the button and not submit the form.</p>\n\n<p>Include the following JavaScript code at the bottom of your ASP.NET web page to make Enter behave the way it should.</p>\n\n<pre><code>// Fixes ASP.NET\'s behavior of default button by testing for more controls\n// than just textarea where the event should not be caugt by the DefaultButton\n// action. This method has to override ASP.NET\'s WebForm_FireDefaultButton, so\n// it has to included at the bottom of the page.\nfunction WebForm_FireDefaultButton(event, target) {\n  if (event.keyCode == 13) {\n    var src = event.srcElement || event.target;\n    if (!(\n      src\n      &amp;&amp;\n      (\n        src.tagName.toLowerCase() == "textarea"\n        || src.tagName.toLowerCase() == "a"\n        ||\n        (\n          src.tagName.toLowerCase() == "input"\n          &amp;&amp;\n          (\n            src.getAttribute("type").toLowerCase() == "submit"\n            || src.getAttribute("type").toLowerCase() == "button"\n            || src.getAttribute("type").toLowerCase() == "reset"\n          )\n        )\n        || src.tagName.toLowerCase() == "option"\n        || src.tagName.toLowerCase() == "select"\n      ) \n    )) {\n      var defaultButton;\n      if (__nonMSDOMBrowser) {\n        defaultButton = document.getElementById(target);\n      }\n      else {\n        defaultButton = document.all[target];\n      }\n      if (defaultButton &amp;&amp; typeof (defaultButton.click) != "undefined") {\n        defaultButton.click();\n        event.cancelBubble = true;\n        if (event.stopPropagation) event.stopPropagation();\n        return false;\n      }\n    }\n  }\n  return true;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/50033', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3617/']