Dear All, I am trying to override the behavior of certain links on a page based on their class. It seemed like this should be easy with Prototype but I ran across some results I don''t understand. When I attach a click event to the link objects with plain innerHTML (e.g. no <b> or <i> tags), the event passes the proper ''a'' object to my function. If the innerHTML of the ''a'' object contains any other objects, such as ''<b>'', it passes that instead. To whit: -------------------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/ TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Element Select Bug Test</title> <script src="lib/prototype.js" type="text/javascript"></script> <script type="text/javascript"> function runTest () { $$(''a.amiabug'').each(function (o) { Event.observe(o, ''click'', foobar); }); } function foobar (e) { Event.stop(e); alert(Event.element(e).inspect()); } Event.observe(window, ''load'', runTest); </script> </head> <body> <a class="amiabug" href="#">Link element with plain text innerHTML</ a> <br /><br /> <a class="amiabug" href="#"><b>Link element with bolded innerHTML</ b></a> </body> </html> ----------------- Clicking the first link returns "<a class=amiabug>" while the second link returns "<b>" Is this is bug, or is there some logic I am missing, and if so, how can I reliably get the ''a'' object passed to my function when clicked? Best Regards, V --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
If you click on the text within a "b" element, then that is the target element of the event. Try working your way up the tree from the target element until you reach the "a" element. Peace, AMA3 ----- Original Message ----- From: "Kodenfreuder" <viktor.nacht-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> To: "Ruby on Rails: Spinoffs" <rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> Sent: Tuesday, May 01, 2007 13:34 Subject: [Rails-spinoffs] Is this a bug or am I missing something? Or both?> > Dear All, > > I am trying to override the behavior of certain links on a page based > on their class. It seemed like this should be easy with Prototype but > I ran across some results I don''t understand. When I attach a click > event to the link objects with plain innerHTML (e.g. no <b> or <i> > tags), the event passes the proper ''a'' object to my function. If the > innerHTML of the ''a'' object contains any other objects, such as ''<b>'', > it passes that instead. To whit: > > -------------------- > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/ > TR/html4/strict.dtd"> > <html> > <head> > <meta http-equiv="Content-Type" content="text/html; > charset=iso-8859-1" /> > <title>Element Select Bug Test</title> > <script src="lib/prototype.js" type="text/javascript"></script> > <script type="text/javascript"> > function runTest () { > $$(''a.amiabug'').each(function (o) { Event.observe(o, ''click'', > foobar); }); > } > function foobar (e) { > Event.stop(e); > alert(Event.element(e).inspect()); > } > > Event.observe(window, ''load'', runTest); > </script> > </head> > <body> > <a class="amiabug" href="#">Link element with plain text innerHTML</ > a> > <br /><br /> > <a class="amiabug" href="#"><b>Link element with bolded innerHTML</ > b></a> > </body> > </html> > ----------------- > > Clicking the first link returns "<a class=amiabug>" while the second > link returns "<b>" > > Is this is bug, or is there some logic I am missing, and if so, how > can I reliably get the ''a'' object passed to my function when clicked? > > Best Regards, > > V > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Michael Peters
2007-May-01 17:45 UTC
Re: Is this a bug or am I missing something? Or both?
Kodenfreuder wrote:> Dear All, > > I am trying to override the behavior of certain links on a page based > on their class. It seemed like this should be easy with Prototype but > I ran across some results I don''t understand.It is easy.> When I attach a click > event to the link objects with plain innerHTML (e.g. no <b> or <i> > tags), the event passes the proper ''a'' object to my function. If the > innerHTML of the ''a'' object contains any other objects, such as ''<b>'', > it passes that instead.When you click on an element, the actual click could be on a child element, but if there are onclick observers they will "bubble up" to each parent that has one. If you want to always get the link then there are a couple of ways that spring to mind. You can use up() or you can use a closure. Using up(): $$(''a.amiabug'').each(function (o) { Event.observe(o, ''click'',foobar) }); function foobar (e) { Event.stop(e); alert(Event.element(el).up(''a.amiabug'').inspect()); } Using a closure: $$(''a.amiabug'').each(function (o) { Event.observe(o, ''click'', function() { Event.stop(e); alert(o.inspect()); }); )); I prefer the closure since it''s shorter and you don''t have to find the element again. -- Michael Peters Developer Plus Three, LP --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you very much for the eerily expedient replies. :) Bubbling was the key concept that didn''t click ''til just now. Best Regards, V --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---