[Dojo-interest] Overriding form submission even when using the Enter key

Andreas Hartmann andreas at apache.org
Fri Apr 24 07:44:01 EDT 2009


Hi Shane,

Shane O'Sullivan schrieb:
> try returning false from the listener function

thanks – now it works!

Just for the record: My <script> element was embedded in an XHTML 
snippet that was loaded using an xhrGet request. It was evaluated by 
Firefox, but to make  it work with Safari, I had to include it in the 
original page.

-- Andreas


> 
> 2009/4/24 Andreas Hartmann <andreas at apache.org>:
>> Hi Shane,
>>
>> Shane O'Sullivan schrieb:
>>> Hi Andreas,
>>>
>>> The idea is not to listen to an onclick or key event.  Instead, you
>>> listen to the <form> element's "onsubmit" event. e.g.
>>>
>>> <form id="myform"> ....
>>>
>>>
>>> dojo.connect(dojo.byId("myform"), "onsubmit", function(evt){
>>>   dojo.stopEvent(evt);
>>> });
>>>
>>> This will stop all normal ways of submitting your form.
>> thanks for clarifying!
>>
>> Now my code looks like this:
>>
>>
>> <form id="comment-form" action="…" method="post">
>>>>   <input type="submit" value="Send"/>
>> </form>
>>
>> <script type="text/javascript">
>>   dojo.connect(dojo.byId("comment-form"), "onsubmit", function(evt) {
>>     dojo.stopEvent(evt);
>>     submitForm('comment-form', …);
>>   });
>> </script>
>>
>>
>> In Firefox everything works fine, submitting the form via xhrPost works
>> with the submit button and the Enter key.
>>
>> However, in Safari the form is still submitted the usual way, no matter
>> if I use the button or the Enter key. Is there something wrong with my code?
>>
>> Thanks for any hints,
>>
>> -- Andreas
>>
>>
>>> Shane
>>>
>>> 2009/4/24 Andreas Hartmann <andreas at apache.org>:
>>>> Hi Matthew,
>>>>
>>>> thanks for your reply!
>>>>
>>>> Matthew Weier O'Phinney schrieb:
>>>>> -- Andreas Hartmann <andreas at apache.org> wrote
>>>>> (on Thursday, 23 April 2009, 04:03 PM +0200):
>>>>>> I have bound a JavaScript function to the submit button of a form,
>>>>>> causing the form to be submitted via an xhrPost request. However, when
>>>>>> the user hits "Enter" in Safari, the form is submitted the usual way.
>>>>>> Firefox doesn't submit the form at all when Enter is pressed.
>>>>>>
>>>>>> Is there any way to prevent Safari from submitting the form when the
>>>>>> Enter key is pressed? Can I somehow bind my JavaScript function to the
>>>>>> enter key instead?
>>>>>>
>>>>>> Thanks a lot in advance!
>>>>> Your JS callback should accept a single parameter, the event. You can
>>>>> then call preventDefault() on the event:
>>>>>
>>>>>     function(e){
>>>>>         e.preventDefault();
>>>>>
>>>>>         // do your handling here...
>>>>>     }
>>>> If you have another minute – do you know a link to a complete example,
>>>> or something to get started? At the moment I'm just calling a custom
>>>> submitForm() function in the onclick attribute of the submit button, but
>>>> I have no idea how to connect this to the Enter key event.
>>>>
>>>> Thanks a lot in advance!
>>>>
>>>> -- Andreas
>>>>
>>>>
>>>> --
>>>> Andreas Hartmann, CTO
>>>> BeCompany GmbH
>>>> http://www.becompany.ch
>>>> Tel.: +41 (0) 43 818 57 01
>>>>
>>>> _______________________________________________
>>>> FAQ: http://dojotoolkit.org/support/faq
>>>> Book: http://dojotoolkit.org/docs/book
>>>> Forums: http://dojotoolkit.org/forum
>>>> Dojo-interest at mail.dojotoolkit.org
>>>> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
>>>>
>>> _______________________________________________
>>> FAQ: http://dojotoolkit.org/support/faq
>>> Book: http://dojotoolkit.org/docs/book
>>> Forums: http://dojotoolkit.org/forum
>>> Dojo-interest at mail.dojotoolkit.org
>>> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
>>>
>>
>> --
>> Andreas Hartmann, CTO
>> BeCompany GmbH
>> http://www.becompany.ch
>> Tel.: +41 (0) 43 818 57 01
>>
>> _______________________________________________
>> FAQ: http://dojotoolkit.org/support/faq
>> Book: http://dojotoolkit.org/docs/book
>> Forums: http://dojotoolkit.org/forum
>> Dojo-interest at mail.dojotoolkit.org
>> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
>>
> _______________________________________________
> FAQ: http://dojotoolkit.org/support/faq
> Book: http://dojotoolkit.org/docs/book
> Forums: http://dojotoolkit.org/forum
> Dojo-interest at mail.dojotoolkit.org
> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
> 


-- 
Andreas Hartmann, CTO
BeCompany GmbH
http://www.becompany.ch
Tel.: +41 (0) 43 818 57 01



More information about the Dojo-interest mailing list