Pretty green on this. Any ideas why this works:
$('.myclass div').append(' <a ">HELLO WORLD</a>').click(this.function);
and this doesn't:
$('.myclass div').after(' <a ">HELLO WORLD</a>').click(this.function);
Thanks!
In both cases, you're adding the handler to the div
that was originally selected. Because events bubble, the first one will still work even tough the handler is on the parent of the <a>
.
If you want the a
element after the div
, you need to bind directly to the a
.
You can use .insertAfter()
instead to accomplish this...
$('<a>HELLO WORLD</a>').insertAfter('.myclass div').click(this.function);