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 the wrap function. 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 the wrap() 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 this odd. thoughts?

Reply via email to