I'm using Alert() just to make sure I have a match between 'Login' and the innerHTML of an element on the page.
var statusElement = document.getElementById('logged-in-status');
var currentLoggedInStatus = statusElement.innerHTML;
alert("innerHTML is: " + currentLoggedInStatus);
When the alert box pops up, it says: innerHTML is: Login
var stringComp = (currentLoggedInStatus == "Login");
alert("stringComp is: " + stringComp);
When this 2nd alert box pops up, it says: stringComp is: false
So the code inside this 'if' statement ERRONEOUSLY executes. I don't see how the first alert() clearly says the innerHTML is 'Login' and yet the code inside the 'if' statement goes right ahead and executes. I tried single-quotes around 'Login' and it did not help.
if(currentLoggedInStatus != "Login")
{
navBarLoginStatusTextElement.innerHTML = 'Login';
}
To reiterate, the innerHTML = Login and when I compare innerHTML to Login it says they're not the same!
Why is my javascript comparison above failing?
EDIT: here is the block element and the php function that sets the value of 'Login':
<a id="logged-in-status"
href="http://localhost/myProj/justAfile.php"><?php echo getLoginStatusStr();?> </a>
and in a php block is the function:
function getLoginStatusStr()
{
return 'Login';
}
var stringComp = (currentLoggedInStatus.indexOf("Login") >= 0);#logged-in-statusseems to end with a whitespace, just as @AlanFoster suspected. Trim it before you compare it.