In the first case you're inserting the link into all the elements that have a class of toggle - thus when you attach the click event it also captures the events from the link. The second one insert the link *before* the elements with a class of toggle, which doesn't bind any event to it.
What you're actually shooting for is this: $('<a href="#" class="toggler">Collapse</a>').click(function() { alert('toggle'); }).insertBefore(".toggle"); --John On Mon, Mar 9, 2009 at 12:36 PM, mwalsh <enov...@gmail.com> 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 jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---