If got a problem using jQuery and I don't know how to fix it. Simply said Ive got the following:
var overlay;
if(condition == "test") {
overlay = $("<div></div>");
}
else if (condition == "test2") {
console.log("first-log");
overlay = $("<span></span>");
}
console.log("second-log");
overlay.prependTo("body");
If the condition matches the 'if', everything works fine, the overlay is filled with content and will prepend to the body.
While the condition matches the 'else if', I get a error that overlay is undefined.
So I've putted some console.log()'s to see what my code is doing. Both console.log action are fired, but in my browser console I'm first seeing the 'second-log' and after that the 'first-log'.
My code is trying prepend before the 'if / else if' is finished, even if the condition matches.
I tried looking for a answer but I couldn't find anything at all so I hope someone here can help me out. Thanks!
You should initialize the var overlay at the beginning of your program.