Jeremy Kitchen
2006-Jan-11 18:34 UTC
extending the Element.prototype to provide a setAttributes function
Hi folks, I''m pretty new to javascript and prototype, so I just want to get some peer-review on a function I wrote that makes a handy (at least, it seems handy) way to set a group of attributes on an element easily. Object.extend(Element.prototype, { setAttributes: function(attrs) { var el = this; $H(attrs).each( function(attr) { el.setAttribute(attr[0], attr[1]); }); } }); You pass it a hash of attr => value pairs, like so: $(''testing'').setAttributes( { test1: "test 1 value", test2: "test 2 value", etc: "hopefully you get the point :)" } I had to use the ''var el = this'' because inside the loop ''this'' was being set to the window object for some reason. Anyone see any problems with that? I have only tested it in firefox 1.5 as of yet, but I will be testing it in other browsers. Thanks! -Jeremy -- Jeremy Kitchen ++ kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org In the beginning was The Word and The Word was Content-type: text/plain -- The Word of Bob. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Jeremy Kitchen
2006-Jan-11 20:09 UTC
Re: extending the Element.prototype to provide a setAttributes function
On Wednesday 11 January 2006 10:34, Jeremy Kitchen wrote:> Hi folks, I''m pretty new to javascript and prototype, so I just want to get > some peer-review on a function I wrote that makes a handy (at least, it > seems handy) way to set a group of attributes on an element easily. > > Object.extend(Element.prototype, { > setAttributes: function(attrs) { > var el = this; > $H(attrs).each( function(attr) { > el.setAttribute(attr[0], attr[1]); > }); > } > });ok, this doesn''t appear to work in konqueror (and presumably not safari, since they''re both khtml based, and I haven''t tested IE or opera yet) so I''ve changed it a bit instead of being an instance method it''s just a method: Object.extend(Element, { setAttributes: function(el,attrs) { el = $(el); $H(attrs).each( function(attr) { el.setAttribute(attr[0], attr[1]); }); } }); call it similar to before: Element.setAttributes( ''elementnameorobject'', { test1: "test 1 value", test2: "test 2 value" } ); On a side note, it appears that firefox is the most liberal about allowing the extension of DOM prototypes, since I had a similar issue previously with trying to add an instance method to the HTMLSelectElement prototype :( Oh well :) -Jeremy -- Jeremy Kitchen ++ kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org In the beginning was The Word and The Word was Content-type: text/plain -- The Word of Bob. _______________________________________________ Rails-spinoffs mailing list Rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails-spinoffs
Martin Bialasinski
2006-Jan-11 22:22 UTC
Re: extending the Element.prototype to provide a setAttributes function
On 11/01/06, Jeremy Kitchen <kitchen-RA8HwDor7flnDGu+y90WmgC/G2K4zDHf@public.gmane.org> wrote:> On a side note, it appears that firefox is the most liberal about allowing the > extension of DOM prototypes, since I had a similar issue previously with > trying to add an instance method to the HTMLSelectElement prototype :(In Internet Explorer, DOM Objects do not have prototypes. At least they are not accessible.