tags:

views:

5086

answers:

7

Well I am trying to submit a form by pressing enter but not displaying a submit button. I don't want to get into JavaScript if possible since I want everything to work on all browsers (the only JS way I know is with events).

Right now the form looks like this:

<form name="loginBox" target="#here" method="post">
    <input name="username" type="text" /><br />
    <input name="password" type="password" />
    <input type="submit" style="height: 0px; width: 0px; border: none; padding: 0px;" hidefocus="true" />
</form>

Which works pretty well. The submit button works when the user presses enter, and the button doesn't show in Firefox, IE, Safari, Opera and Chrome. However, I still don't like the solution since it is hard to know whether it will work on all platforms with all browsers.

Can anyone suggest a better method? Or is this about as good as it gets?

+1  A: 

AFAIK, browsers won't set focus to hidden elements -- though, I'm not sure if it'll then disable [Enter] submitting (FF3 and IE7 work).

<input type="submit" style="display: none;" />
Jonathan Lonowski
Doesn't work on IE6 (which might be a big deal).
Ates Goral
or chrome - i think this idea is out
Simon_Weaver
+1  A: 

Instead of the hack you currently use to hide the button, it would be much simpler to set visibility: collapse; in the style attribute. However, I would still recommend using a bit of simple Javascript to submit the form. As far as I understand, support for such things is ubiquitous nowadays.

Noldorin
Doesn't hide it on IE6.
Ates Goral
Ah yes, it's always IE6 forcing us to do nasty hacks...
Noldorin
+1  A: 

You could try also this

<INPUT TYPE="image" SRC="0piximage.gif" HEIGHT="0" WIDTH="0" BORDER="0">

You could include nonexistent image, or image with width/height = 0 px

waney
+5  A: 

Try:

<input type="submit" style="position: absolute; left: -9999px"/>

That will push the button waaay to the left, out of the screen. The nice thing with this is, you'd get graceful degradation when CSS is disabled.

Ates Goral
I'll try that, thanks!
Anyone having trouble with this in IE 7? I get a huge submit bar that runs the entire width of the page with this solution. I've had to go the JS route as a result. Any suggestions?
Erebus
A: 

Have you tried this ?

<input type="submit" style="visibility: hidden;" />

Since most browsers understand visibility:hidden and it doesn't really work like display:none, I'm guessing that it should be fine, though. Haven't really tested it myself, so CMIIW.

andyk
thanks strager for the edit.
andyk
+2  A: 

I think you should go the Javascript route, or at least I would:

<script type="text/javascript">
// Using jQuery.

$(function() {
    $('form').each(function() {
        $('input').keypress(function(e) {
            // Enter pressed?
            if(e.which == 10 || e.which == 13) {
                this.form.submit();
            }
        });

        $('input[type=submit]').hide();
    });
});
</script>


<form name="loginBox" target="#here" method="post">
    <input name="username" type="text" /><br />
    <input name="password" type="password" />
    <input type="submit" />
</form>
strager
nice one, tested and working fine. But before trying something like this, remember that submitting form via Javascript won't cause some browsers to offer the password saving stuff.
andyk
+1  A: 

I can't think of any reason to hide the submit button. It doesn't look like a good idea from a usability point of view.

Kristof Neirynck
It is a quick login box and having the button clutters that.
@PythonPower, It's still good to have a button regardless. You can style it to make it compact, or even use an image. "Go" is often used/acceptable if the login box is already labeled.
strager
agree with strager. There's still a handful of people that must reach for their mouse after entering anything into a web form. But it's always good to know ways of doing something.
andyk
since I am web savvy i try to avoid pressing enter in forms (subconsciously mostly) becasue i know of the surrounding issues and dont always expect it to work reliably. just a little arrow pointing to the right in a circle is what i'd do
Simon_Weaver