Hi, I''m trying to convert some jquery code to Prototype. Basically I want a click anywhere in the document other than the menu to close any open menu windows. in jQuery, it''s this: $(document).click(function(event){ var target = $(event.target); if (target.parents("#menu").length == 0) { hide(''all''); } }); in Prototype, I have this: Element.observe(document, ''click'', doThis); function doThis(event){ var target = $(event.target); alert(target); } But how do I access the id and/or parent of the target element, so I can determine whether or not to close the menu windows? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
In Prototype, you can get the target element itself this way: function doThis(evt){ var targetElement = Event.element(evt); alert(targetElement.target); alert(targetElement.up(''div'').id); //or something like that } (I have a sneaking suspicion that using event as the variable name might cause some pain in IE. I usually stick to evt, but that''s probably cargo cult on my part...) Walter On Apr 15, 2008, at 10:03 AM, Ian wrote:> in Prototype, I have this: > > Element.observe(document, ''click'', doThis); > function doThis(event){ > var target = $(event.target); > alert(target); > } > > > But how do I access the id and/or parent of the target element, so I > can determine whether or not to close the menu windows?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Oh, it''s really simple: document.observe(''click'', function(e) { // traverse up looking for element that matches selector [1] if (e.target.up(''#menu'')) { hide(''all''); } }) [1] http://www.prototypejs.org/api/element/up - kangax On Apr 15, 10:03 am, Ian <ian.2...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote:> Hi, I''m trying to convert some jquery code to Prototype. Basically I > want a click anywhere in the document other than the menu to close any > open menu windows. > > in jQuery, it''s this: > > $(document).click(function(event){ > var target = $(event.target); > if (target.parents("#menu").length == 0) { > hide(''all''); > } > }); > > in Prototype, I have this: > > Element.observe(document, ''click'', doThis); > function doThis(event){ > var target = $(event.target); > alert(target); > } > > But how do I access the id and/or parent of the target element, so I > can determine whether or not to close the menu windows?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thanks so much. This is just what I needed to know. On Apr 15, 11:33 am, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Oh, it''s really simple: > > document.observe(''click'', function(e) { > // traverse up looking for element that matches selector [1] > if (e.target.up(''#menu'')) { > hide(''all''); > } > > }) > > [1]http://www.prototypejs.org/api/element/up > > - kangax > > On Apr 15, 10:03 am, Ian <ian.2...-/E1597aS9LQAvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, I''m trying to convert some jquery code to Prototype. Basically I > > want a click anywhere in the document other than the menu to close any > > open menu windows. > > > in jQuery, it''s this: > > > $(document).click(function(event){ > > var target = $(event.target); > > if (target.parents("#menu").length == 0) { > > hide(''all''); > > } > > }); > > > in Prototype, I have this: > > > Element.observe(document, ''click'', doThis); > > function doThis(event){ > > var target = $(event.target); > > alert(target); > > } > > > But how do I access the id and/or parent of the target element, so I > > can determine whether or not to close the menu windows?--~--~---------~--~----~------------~-------~--~----~ 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 Apr 16, 12:35 am, Walter Lee Davis <wa...-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> In Prototype, you can get the target element itself this way: > > function doThis(evt){ > var targetElement = Event.element(evt); > alert(targetElement.target); > alert(targetElement.up(''div'').id); > //or something like that > > } > > (I have a sneaking suspicion that using event as the variable name > might cause some pain in IE. I usually stick to evt, but that''s > probably cargo cult on my part...)Identifiers included in the parameter list of a function declaration create local variables of the same name as if declared within the function. A function declaration like: function foo(event) {...} may cause a problem is where it is expected that within the function the unqualified use of the identifier event will resolve to the global window.event property and not the local event variable. Since window.event is a maifestation of the IE event model and there are many browsers that implement a different event model (typically Mozilla''s), this issue is normally resolved as a consequence of resolving the differences in event models, e.g. function foo(event) { event = event || window.event; ... } Although I typically use ''e'' rather than event anyway. :-) -- Rob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---