I create a span with a global variable like this:
var $span = jQuery('<span></span');
jQuery($span).append('<img src="myimage.gif"/>');
jQuery($span).insertAfter('#username');
It works but I would like to remove the span in some part of my code.
I try with:
$span.remove();
but it doesn’t work.
Thanks!
You really need to post the full code for questions like this. It makes it really difficult for people to help you if they can’t see what you’re doing in context.
That said, if
$span.remove()does not remove the<span>, then plainly and simply$spanis not referencing what you think it is. Either it’s another element or it’s undefined.Where are you making it a “global” variable (bad idea by the way)? In javascript there really isn’t such a thing as a true global variable — they are only defined for for the context they are in. If the immediate context is your document (i.e.
<script>var myVar = 'foo';</script>), that’s for all intents and purposes a “global” variable, but anything else is a “local” variable.Basically, use
console.logor similar to introspect the value of$spanin the place where you’re trying to utilize it. If it’s undefined (which my best guess is that it is), then figure out why it’s not defined in the context your working in.