Ryan Gahl
2006-Jan-11 18:50 UTC
RE: extending the Element.prototype to provide asetAttributes function
No comment on the usefulness of your function, as I''ve yet to try it.
But for the "this" scope problem, try this... (removed your var and
added the bind(this) after the function declaration. I didn''t test it
though so you might want to give it whirl. It might introduce a
different scope problem with the "attr" var but I think it should
work.
Object.extend(Element.prototype, {
setAttributes: function(attrs) {
$H(attrs).each( function(attr) {
this.setAttribute(attr[0], attr[1]);
}.bind(this));
}
});
-----Original Message-----
From: rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
[mailto:rails-spinoffs-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org]
On Behalf Of
Jeremy Kitchen
Sent: Wednesday, January 11, 2006 12:34 PM
To: rails-spinoffs-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
Subject: [Rails-spinoffs] extending the Element.prototype to provide
asetAttributes 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.
The information transmitted in this electronic mail is intended only for the
person or entity to which it is addressed and may contain confidential,
proprietary, and/or privileged material. Any review, retransmission,
dissemination or other use of, or taking of any action in reliance upon,
this information by persons or entities other than the intended recipient
is prohibited. If you received this in error, please contact the sender and
delete the material from all computers.
Jeremy Kitchen
2006-Jan-11 18:58 UTC
Re: extending the Element.prototype to provide asetAttributes function
On Wednesday 11 January 2006 10:50, Ryan Gahl wrote:> No comment on the usefulness of your function, as I''ve yet to try it. > But for the "this" scope problem, try this... (removed your var and > added the bind(this) after the function declaration. I didn''t test it > though so you might want to give it whirl. It might introduce a > different scope problem with the "attr" var but I think it should work.I''ll give this a try, thanks for your comment :) -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 19:01 UTC
Re: extending the Element.prototype to provide asetAttributes function
On Wednesday 11 January 2006 10:58, Jeremy Kitchen wrote:> On Wednesday 11 January 2006 10:50, Ryan Gahl wrote: > > No comment on the usefulness of your function, as I''ve yet to try it. > > But for the "this" scope problem, try this... (removed your var and > > added the bind(this) after the function declaration. I didn''t test it > > though so you might want to give it whirl. It might introduce a > > different scope problem with the "attr" var but I think it should work. > > I''ll give this a try, thanks for your comment :)great! that worked. Thanks again :) -Jeremy ps, sorry for the quick self-reply, I was a bit quick-to-fire on the send button :) -- 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
Possibly Parallel Threads
- extending the Element.prototype to provide a setAttributes function
- prototype-ish subclassing
- prototype.js Event.stopObserving
- [prototype] Selector class fails on selector strings with spaces
- RE: question about auto-updating div withoverflow:auto and maintaining position