$('.toggle').[before|prepend]($('<a href="#"
class="toggler">Collapse</a>').click(function() {
alert('toggle');
}));
.prepend and .append don't return the a created element. The general
idea of .append, etc... is "Insert this stuff inside of me, then return
me so that more stuff can be done on me."
If you want to attach things to the node you created, you should either
nest a simple $(html).dosomething() which will return then be inserted,
or use something like .appendTo, .prependTo, etc... which work in the
opposite direction and inject the current object relative to something
else, instead of injecting something relative to the current object.
Basically the one thing to understand is when thinking of any dom
manipulation (append, prepend, appendTo, before, etc...) the chainable
object returned is the same context you started with, NOT what you inserted.
It was a little to get used to migrating from my own framework which
worked like pf(...).append(nodeName, attrs).doSomethingOnNewNode(); but
once I got used to it I found that nesting rather than chaining with the
new object was actually a lot more powerful.
~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://nadir-seen-fire.com]
-Nadir-Point & Wiki-Tools (http://nadir-point.com) (http://wiki-tools.com)
-MonkeyScript (http://monkeyscript.org)
-Animepedia (http://anime.wikia.com)
-Narutopedia (http://naruto.wikia.com)
-Soul Eater Wiki (http://souleater.wikia.com)
mwalsh wrote:
> Hi,
>
> I'm puzzled why this script works:
>
> $('.toggle').prepend('<a href="#" class="toggler">Collapse</a>').click
> (function() {
> alert('toggle');
> });
>
> But this one doesn't:
>
> $('.toggle').before('<a href="#" class="toggler">Collapse</a>').click
> (function() {
> alert('toggle');
> });
>
> I don't understand why!
>
> Thanks for looking.
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"jQuery Development" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---