I am trying to compare 2 strings, but there are not comparing as I would like. I am getting a value from a div and converting it to a string, when I find the typeof it is telling me is a string but still it is passing by the if statement.
When I write.document they both look the same. Thanks
<div id="countries"><?php $geoplugin->locate(); echo"{$geoplugin->countryName}"?> </div>
<script>
var country = document.getElementById("countries");
var count = country.innerHTML;
var newc = String(count);
if ( newc === "Ireland" ){
document.write("this is ireland");
alert("You are from Ireland");
}else{
document.write("You Live in " + newc);
//alert(typeof newc);
//alert(newc);
}
</script>
From your HTML it looks like you've got a trailing space after the <?php ...>
instruction. So whatever is there, it won't exactly equal "Ireland"
. Try using trim
to remove any whitespace from the ends of your string.
var newc = String(count).trim();