Stopping IE from highlighting the first submit-button in a form

The expected behaviour on enter in a form seems to be undefined in the HTML 4.01 Specification while the HTML 5 specification says it should be the first submit button in the form.

Internet Explorer (IE) highlights the first button in the form when the form has focus by adding a proprietary border or something. Other browsers do not add such a visual clue.

I’d like to use another button as the default and style this button so all browsers will provide a visual clue that it is the default button. (We’re using ASP.NET which uses one form per page, and so it’s hard to design the pages so that the default button always comes first.)

While I can easily accomplish this with javascript and css in most browsers, I’m having trouble making IE stop highlighting the first button.

Is there any way to tell IE to NOT highlight the first submit-button or to highlight a different button? Or is there some other solution that I’ve missed?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

On your asp.net button control, set useSubmitBehavior="false". That renders the html as a button rather than a submit.

Method 2

ASP.Net 2.0 also introduced the concept of DefaultButton.

This is available on atleast Form and Panel elements:

<form id="form1" runat="server" defaultbutton="Button2">
<div>
    <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
    <br />
    <asp:Button id="Button1" runat="server" text="1st Button" onclick="Button1_Click" />
    <br />
    <br />
    <asp:panel id="something" defaultbutton="button3" runat="server">
        <asp:TextBox id="TextBox2" runat="server"></asp:TextBox>
        <br />
        <asp:Button id="Button2" runat="server" text="2nd Button" onclick="Button2_Click" />
        <br />
        <asp:Button id="Button3" runat="server" text="3rd Button" onclick="Button3_Click" />

    </asp:panel>
    <br />
    <br />

</div>
</form>

So, when you load the page, or are entering text in TextBox1, pressing Enter will submit Button2. When you are entering text in TextBox2 pressing Enter will submit Button3 – as you are inside the Panel.

This is powered by an onkeypress method created by ASP.Net.

When the page loads, IE and Firefox both highlight Button2 (as you want).

If you know which button will be declared as the defaultbutton, you can use what ever CSS you would normally use to style this for all browsers.

Rather annoyingly, when you focus either text box, IE will then (incorrectly) highlight Button1.

This is because IE’s default behaviour is overridden by some javascript emitted by ASP.Net:

<script type="text/javascript">
//<![CDATA[
WebForm_AutoFocus('Button2');//]]>
</script>

But, IE will then ignore that once another element has focus, and go back to highlighting the wrong button (which doesn’t get used unless the user explicitly clicks it in this example).

Other than replacing them with image buttons, I’m not sure what else I can suggest.

Method 3

One way to get around this is to create a dummy button at the top of the form and move it out of sight. Then handle the enter keycode in javascript and style the new default button with css.

Feels dirty though. Any better ideas?

Method 4

Use either of these CSS Styles

a:active, a:focus,input, input:active, input:focus{     outline: 0;     outline-style:none;     outline-width:0; }  
a:active, a:focus,button::-moz-focus-inner, input[type="reset"]::-moz-focus-inner, input[type="button"]::-moz-focus-inner, input[type="submit"]::-moz-focus-inner, input[type="file"] > input[type="button"]::-moz-focus-inner 
{     border: none; }

OR

:focus {outline:none;} ::-moz-focus-inner {border:0;}

Once the CSS Style part is done, then you might also need to set the IE-Emulator. Update your web applications web.config file and include below key.

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <clear />
        <add name="X-UA-Compatible" value="IE=7; IE=9; IE=8; IE=5;" />
      </customHeaders>
    </httpProtocol>

  </system.webServer>

Method 5

If you are using .Net 2 this may help you

…Use the DefaultFocus property to
access the control on the form to
display as the control with input
focus when the HtmlForm control is
loaded. Controls that can be selected
are displayed with a visual cue
indicating that they have the focus
.
For example, a TextBox control with
focus is displayed with the insertion
point positioned inside of it. …

Method 6

What about defining a particular CSS style (or class) for this button ?

<input type="button" value="basic button" .../>
<input type="button" style="border: solid 2px red;" value="default button" .../>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x