I want to create and append an item in jquery, while saving a reference to it.
var buy = "<img src='img/buy-now.png' />";
var $buy = $(buy).appendTo("body");
$buy.html("hello");
I was expecting something like the above to work. Any ideas?
In short,
$buyis the object you’ve appended, you’re just doing an invalid operation.<img />is a self-closing tag, there is no HTML inside it, so.html("something")will have no effect.If you meant to set the tooltip, use
.attr(), like this: