I need a solution for the age old problem of a 'default button' firing undesirably. i.e you hit enter in a text box, but there is a submit button on the form that isn't the one you want to fire (or maybe you don't want the form to fire at all).
I'm wondering about the following 'solution'. Slightly hacky but should be reliable as far as I can tell.
Inside the form the FIRST thing is a button which is invisible. Then some jquery to immediately disable it. If you hit enter on the form this button counts as the 'default button' and gets triggered, but does nothing because of the 'return false' event handler.
Solutions I've seen before rely on things like keydown event handlers, or other seemingly complex / hard to test in every browser.
My solution (that I haven't seen before but is probably not unique) seems much simpler and I think pretty reliable. You can even tell if javascript was disabled and someone hit enter because the server will receive this button in the form data.
<form action="/store/checkout" method="post">
<input id="btnFakeSubmit" name="FakeSubmit" src="/images/pixel.gif"
style="width:1px; height:1px; position:absolute;" type="image" />
<script>
$('#btnFakeSubmit').click(function() {
return false;
});
</script>
Any advice on this solution - including the best way to hide the button in all browsers.
One thing you could do is:
javascript: return false;
submit()
on the form object.e.g. something like this:
This way, there's no way to submit the form other than explicitly hitting this button.