I have been trying to understand the working of Event.observe() as applied to submission of a form (I need to catch form submission, without passing it to action script), but was unable to understand why assuming this HTML: <form id="myform" name="form" action="#"> <input type="text" id="myinput" name="input" /> </form> the following code does not work [1]: Event.observe(''myform'',''submit'',function(el){ Event.stop(el); alert(''Form submit''); },false); However, if I tell Javascript to observe onSubmit for the window, like so [2]: Event.observe(window,''submit'',function(el){ Event.stop(el); alert(''Form submit''); },false); it all starts to work... When I am tracing execution of [1] in Drosera (or Venkman, for that matter), I see that for some reason $(''myform'') returns undefined (this is n prototype.js 1.5.0_rc0). However, CSS does not seem to have any problem styling it. -- A. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Andrei Popov wrote:> ... > the following code does not work [1]: > > Event.observe(''myform'',''submit'',function(el){ > Event.stop(el); > alert(''Form submit''); > },false);I sounds like you have the need for bindAsEventListener([obj]) Event.observe(''myform'',''submit'',function(eventObj) { Event.stop(eventObj); alert(''Form submit''); }.bindAsEventListener(),false); --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On 11/14/06, Andrei Popov <ceesaxp-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> When I am tracing execution of [1] in Drosera (or Venkman, for that > matter), I see that for some reason $(''myform'') returns undefined > (this is n prototype.js 1.5.0_rc0). However, CSS does not seem to > have any problem styling it.Looks like you have the script before the ''myform'' element in your document. Also Ken is right about .bindAsEventListener(). --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Christophe Porteneuve
2006-Nov-15 08:00 UTC
Re: Event.observe for onSubmit event of a form
Martin Bialasinski a écrit :> On 11/14/06, Andrei Popov <ceesaxp-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> When I am tracing execution of [1] in Drosera (or Venkman, for that >> matter), I see that for some reason $(''myform'') returns undefined >> (this is n prototype.js 1.5.0_rc0). However, CSS does not seem to >> have any problem styling it. > > Looks like you have the script before the ''myform'' element in your > document. Also Ken is right about .bindAsEventListener().Your diagnosis is most likely correct. There''s no way for $() to return undefined if the element with this exact ID appeared in the DOM before. This is most likely a case of the observation happening too early. So Andrei: either put it in a script tag later in the doc (bleah!), or create a page-init function that contains this code and make this function observe window''s load event. As for bAEL: this is only true if the function is a method and needs proper "this" access. In the sample case Andrei provided, this has no incidence whatsoever. Oh, and Andrei: your HTML example has no means of regular submission for the form: neither submit- nor image-typed input''s. So submission relies on some JS mechanism? FWIW, remember that''s inaccessible (I know, you use JS to short-circuit it anyway, but hey...). -- Christophe Porteneuve a.k.a. TDD "[They] did not know it was impossible, so they did it." --Mark Twain Email: tdd-x+CfDp/qHev2eFz/2MeuCQ@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---