yea! weird huh?

yes the workarounds do work (and there's even a few more that i can
think of) but i was simply scratching my head when i came up against
the problem.

ill see if i can make sense of the source, and perhaps submit a ticket
so that someone with a better understanding can give it a look. thanks
for your responses ricardo.


On Sep 25, 10:04 pm, ricardobeat <[EMAIL PROTECTED]> wrote:
> Hmmm...
>
> Actually, after some tests I found my idea of this to be false. You
> can indeed attach event handlers before an element is inserted into
> the DOM. Sorry :D
>
> But I don't know what's weirder: this construct for wrapping/attaching
> a handler at the same time or that the wrap() function prevents the
> event function from being attached. I'm not sure it's technically a
> bug, but the elements do seem to lose it's bound functions when passed
> to the wrap() function. There must be a reason for that, like the
> element having to be recreated and it's impossible to recover
> functions bound to events, but I haven't looked at the code.
>
> Anyway, hope the alternatives I showed can be useful.
>
> cheers,
> - ricardo
>
> On 26 set, 01:28, darren <[EMAIL PROTECTED]> wrote:
>
> > i don't see how creating the element, assigning the handler, and
> > replacing or appending something works... yet, creating the element,
> > assigning the handler, and wrapping it doesn't.
> > can you help me understand what the difference is?
>
> > On Sep 25, 5:39 pm, ricardobeat <[EMAIL PROTECTED]> wrote:
>
> > > It doesn't work for you because my code is not missing a ), actually I
> > > made a mistake on rewriting it. I'm assigning the click handler to the
> > > 'b' element which was wrapped, it's not inside thewrapfunction. It
> > > should read:
>
> > > $('b').wrap('<a href="#"></a>').click(function(){
> > >     alert("foo");
>
> > > });
>
> > > The second snippet I posted assigns it to the <a> that was created but
> > > it's not really needed.
>
> > > $('b').wrap('<a href="#"></a>').parent().click(function(){
> > >     alert("foo");
>
> > > });
>
> > > As I said before, you can't attach an event handler to the element
> > > before it is appended to the DOM. While you write the click() function
> > > inside thewrap() it will never work.
>
> > > - ricardo
>
> > > On Sep 25, 4:58 pm, darren <[EMAIL PROTECTED]> wrote:
>
> > > > sorry for the double post, not sure how that happened.
>
> > > > ricardo, your code is missing a closing ) at the end. it should be...
> > > > $('b').wrap($('<a href="#"></a>').click(function(){
> > > >     alert("foo");
>
> > > > }));
>
> > > > your element creation: $('<a href="#"></a>')
> > > > is equivalent to the shorthand i used: $("<a href='#'/>")
>
> > > > in either case, the click handler does not get assigned for me, so i
> > > > am not sure how it works for you?
>
> > > > On Sep 24, 1:30 pm, ricardobeat <[EMAIL PROTECTED]> wrote:
>
> > > > > I believe you can't assing an event handler to an element before it is
> > > > > added to the DOM. This works for me:
>
> > > > > $('b').wrap($('<a href="#"></a>').click(function(){
> > > > >     alert("foo");
>
> > > > > });
>
> > > > > Or if you want the click event assigned to <a> and not <b>
>
> > > > > $('b').wrap($('<a href="#"></a>').parent().click(function(){
> > > > >     alert("foo");
>
> > > > > });
>
> > > > > - ricardo
>
> > > > > On Sep 24, 3:08 pm, darren <[EMAIL PROTECTED]> wrote:
>
> > > > > > i just want to bring this up for discussion to see what people have 
> > > > > > to
> > > > > > say about it and to further my understanding of how jquery works and
> > > > > > why it works that way. maybe its a bug?
>
> > > > > > imagine the base code <b>Hello World!</b>
>
> > > > > > // fails to assign click handler:
> > > > > >$("<a href='#'/>").click(function(){
> > > > >     alert("foo");
> > > > > })
>
> > > > > > // also fails... wrapAll, wrapInner etc
>
> > > > > > // works as expected:
> > > > > > $("b").replaceWith($("<a href='#'>Hello 
> > > > > > World</a>").click(function(){
> > > > > >     alert("foo");}));
>
> > > > > > // also works as expected: html, prepend, append, after, before,
> > > > > > etc...
>
> > > > > > i found thisodd. thoughts?

Reply via email to