$("a")
is actually an object, not a string. If you use $("div").after(link)
, jQuery will work out that you actually want to append the DOM element.
The problem comes in when you do '<div>' + link + '</div>'
, where JavaScript is creating the string before jQuery gets involved. this is where [object Object]
comes from - this is JavaScript's way of creating a sensible String value for an object. What's being evaluated is $("div").after("<div>[object Object]</div>");
You can get around this by first creating your new div
, appending the a
to that, then appending your new div to the original.
$(document).ready(function() {
var link = $("a"),
new_div = $("<div />").append(link);
$("div").after(new_div);
});