Hi , I have been trying to extend prototype''s DOM extensions to a collection of radio button inputs with no success whatsoever. Here is the code: -------------------------------------------------- var profiles = Form.getInputs("Form1", "radio", "supporter_id"); profiles.each ( function(node) { Event.observe ( node, ''click'', function () { var tds = this.up(1).immediateDescendants(); Cookie.set("test", ''FirstName=''+tds[3].innerHTML +''&LastName=''+tds[4].innerHTML, 1); alert(Cookie.get("test")); return false; },false ); }); ------------------------------------------------------- which trips up IE. I have tried every which way to extend this, including but not limited to: ---------------- profiles.each(function() { Element.extend(profiles.each); }); ----- profiles.each(Element.extend).invoke ----- for (i=0;i<profiles.length;i++) { Element.extend(profiles[i]); } --------- Element.extend(this); ----- Nothing seems to work. Now if I name a specific instance of my collection: ------------------------------ var profiles = Form.getInputs("Form1", "radio", "supporter_id"); var profile = profiles[0].up(1).immediateDescendants(); Cookie.set("test", ''FirstName=''+profile[2].innerHTML +''&LastName=''+profile[3].innerHTML, 1); alert(Cookie.get("test")); ------------------------------------- It works fine. But I am trying to loop through an array, and build a listener. Any ideas? --~--~---------~--~----~------------~-------~--~----~ 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 Oct 3, 11:19 am, rfd <rfd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi , > > I have been trying to extend prototype''s DOM extensions to a > collection of radio button inputs with no success whatsoever. > > Here is the code: > -------------------------------------------------- > var profiles = Form.getInputs("Form1", "radio", "supporter_id"); > > profiles.each ( function(node) > { > Event.observe ( node, ''click'', function () > { > > var tds = this.up(1).immediateDescendants();Because when you attach event listeners this way, the value of the function''s this keyword is set differently in IE than most other browsers. In IE, it will be the window object, in others it will be the element that the listener is attached to. You pass node to the function, so use it. You may also find that using the cells collection is quicker than getting the immediate desecendants (though you may not notice that). var tds = node.up(1).cells; It uses the DOM cells collection rather than Prototype''s iteration over child nodes. You may also find that you have memory leaks in IE due to closures and circular references, but that may not matter. -- 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 -~----------~----~----~----~------~----~------~--~---
On 2 oct, 21:19, rfd <rfd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi , > > I have been trying to extend prototype''s DOM extensions to a > collection of radio button inputs with no success whatsoever. > > Here is the code: > -------------------------------------------------- > var profiles = Form.getInputs("Form1", "radio", "supporter_id"); > > profiles.each ( function(node) > { > Event.observe ( node, ''click'', function () > { > > var tds = this.up(1).immediateDescendants(); > > Cookie.set("test", ''FirstName=''+tds[3].innerHTML > +''&LastName=''+tds[4].innerHTML, 1); > > alert(Cookie.get("test")); > > return false; > > },false );}); > > ------------------------------------------------------- >Like Rob said, you should use ''node.up'' instead of ''this.up'' as the DOMElement (node) is passed in your iteration function, and is thus available because of closures in Javascript. Another way would be to bind the event function with the node element. I.e. : Event.observe( node, ''click'', function() { .... }.bind(node), 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 -~----------~----~----~----~------~----~------~--~---
> > var tds = this.up(1).immediateDescendants(); > var tds = node.up(1).cells;I let out a massive DOO''H when I read this. I implemented the .cells collection as well. I can''t notice any speed difference, but it just seems more appropriate. Thank you! On Oct 2, 10:19 pm, RobG <rg...-AFFH1GffN5hPR4JQBCEnsQ@public.gmane.org> wrote:> On Oct 3, 11:19 am, rfd <rfd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi , > > > I have been trying to extend prototype''s DOM extensions to a > > collection of radio button inputs with no success whatsoever. > > > Here is the code: > > -------------------------------------------------- > > var profiles = Form.getInputs("Form1", "radio", "supporter_id"); > > > profiles.each ( function(node) > > { > > Event.observe ( node, ''click'', function () > > { > > > var tds = this.up(1).immediateDescendants(); > > Because when you attach event listeners this way, the value of the > function''s this keyword is set differently in IE than most other > browsers. In IE, it will be the window object, in others it will be > the element that the listener is attached to. > > You pass node to the function, so use it. You may also find that > using the cells collection is quicker than getting the immediate > desecendants (though you may not notice that). > > var tds = node.up(1).cells; > > It uses the DOM cells collection rather than Prototype''s iteration > over child nodes. > > You may also find that you have memory leaks in IE due to closures and > circular references, but that may not matter. > > -- > 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 -~----------~----~----~----~------~----~------~--~---
IE doesn''t automatically extend elements when they are returned in a collection (an array). You need to $() node whenever you see it. Check this page for more info: http://prototypejs.org/learn/extensions var profiles = Form.getInputs("Form1", "radio", "supporter_id"); profiles.each ( function(node) { Event.observe ( $(node), ''click'', function () // IE doesn''t automatically extend the elements as they are returned { var tds = $(node).up(1).immediateDescendants(); Cookie.set("test", ''FirstName=''+tds[3].innerHTML +''&LastName=''+tds[4].innerHTML, 1); alert(Cookie.get("test")); return false; },false ); }); On Oct 3, 2:19 am, rfd <rfd...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi , > > I have been trying to extend prototype''s DOM extensions to a > collection of radio button inputs with no success whatsoever. > > Here is the code: > -------------------------------------------------- > var profiles = Form.getInputs("Form1", "radio", "supporter_id"); > > profiles.each ( function(node) > { > Event.observe ( node, ''click'', function () > { > > var tds = this.up(1).immediateDescendants(); > > Cookie.set("test", ''FirstName=''+tds[3].innerHTML > +''&LastName=''+tds[4].innerHTML, 1); > > alert(Cookie.get("test")); > > return false; > > },false );}); > > ------------------------------------------------------- > > which trips up IE. I have tried every which way to extend this, > including but not limited to: > > ---------------- > profiles.each(function() { > Element.extend(profiles.each); > }); > > ----- > > profiles.each(Element.extend).invoke > > ----- > > for (i=0;i<profiles.length;i++) > { > Element.extend(profiles[i]); > > } > > --------- > > Element.extend(this); > > ----- > > Nothing seems to work. Now if I name a specific instance of my > collection: > > ------------------------------ > var profiles = Form.getInputs("Form1", "radio", "supporter_id"); > > var profile = profiles[0].up(1).immediateDescendants(); > > Cookie.set("test", ''FirstName=''+profile[2].innerHTML > +''&LastName=''+profile[3].innerHTML, 1); > > alert(Cookie.get("test")); > ------------------------------------- > > It works fine. But I am trying to loop through an array, and build a > listener. > > Any ideas?--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---