views:

130

answers:

4

I have jQuery go though all the submit buttons with the class button, and turn them each into an anchor element with the class button.

$("input.button").each(function(i) {

    var anchorButton = $("<a class='"+this.className+"' href='#'>"+this.value+"</a>")



    anchorButton.click(function(eventObject) {

        $(this).blur().prev("input.hidden").click();

        return false;

    });



    $(this)

        .after(anchorButton)

        .addClass("hidden")

        .removeClass("button pill-left pill-center pill-right");

});

I can then style the anchor element with CSS, cross-browser-ly. There's only one problem. Whenever I press the Enter key on form input elements, the form doesn't submit. However, if I un-hide the submit button, it works again. In conclusion, I can't get the Enter/Return key to submit the form unless I have a submit button visible (display != none).

How can I get the form to submit when I press Enter when I have the submit button hidden?

A: 

Have you tried changing the visibility to hidden instead of display to none?

Rupert
even i think that should work - you beat me to it :D
Roopesh Shenoy
A: 

Try visibility=hidden

Roopesh Shenoy
+2  A: 
$("input.button").keydown(function(event) {
   if(event.keyCode == 13)
      $("...find the form...").submit();
});

This should work.

If it doesn't try this:

   $("..find the input button you hide..").css('position', 'absolute')
         .css('top', '-1000px');

This is better than the proposed visibility: hidden solutions since if you set the visibility to hidden the button will still "take up space".

Andreas Bonini
Okay admitted - though the question specifically asked about hiding the submit button :)
Roopesh Shenoy
@Roopesh: The question was how to make the form submit by pressing the Enter key when the *submit button was already hidden*.
Stian
True the visibility CSS property is not appropriate.I already have the code that submits the form when I press the anchor element. What I need is to keep the Enter key's default behavior on input[type=text] elements.I guess my solution would be to use absolute positioning instead of the display property. I'll give it a try.
Sam
@Andreas: Shouldn't the selector be `input[type=text]` instead of `input.button` ?
Stian
@Stain, not at all. First, input[type=text] is for text boxes; I believe you meant input[type=button], but this would work in IE6. The reason I use a class so for IE compatibility (IE cannot recognize CSS attribute selectors).I gave the absolute positioning alternative a try. It's definitely a solution! However, I'm still open to other solutions.
Sam
My suggestion is to use andreas' code but instead use $('form').delegate('*',keydown', callback'). This should only fire when the form is focused. The follow up code is the same.
jpluijmers
A: 

Just use an ordinary form in your HTML, including a submit action attribute. Then you'll get the browser's default behaviour and pressing Enter within the form will submit it. Then you don't have to use jQuery to bind event handlers to all your submit buttons.

You can test it with this JSBin.

Marcel Korpel