i've started learning about javascript closures, and while experimenting, i realised that the following code does not work as expected:
(function($){
var p='<p style="color:red">12345</p>';
$p=$(p);
$("body").append($p);
$p.appendTo($("body"));
console.log($p);
})(jQuery)
in the console, i can see the jquery object being returned, but its not being appended to the body (i have tried both the append and appendto methods).
can someone please explain to me why this code does not work as expected?
i had a thought: this may have been because jquery is not loaded by the time this function is called, however, the jquery object IS being output to the console, so that must mean jquery IS loaded at the time that this function is called.
can any knowledgeable person shed some light?