Kajcioch Kajcioch - 3 months ago 8
Javascript Question

If statement has trouble with text();

who = $(".tekst").text();

if (who=="XxX") {
$("#discu").css("display", "none");
}

alert(who);


So my
.tekst
is:
<a class="tekst" href="#">XxX</a>
and when I call a
text()
function, I get printed out
XxX
just like I want (I can see that in
alert();
box). The problem is, that my
if
statement doesn't work. It's output is just
false
. Why?

Answer

Remove the trailing and leading whitespace by using String#trim or jQuery.trim() method.

who = $(".tekst").text().trim();

if (who=="XxX") {
    $("#discu").css("display", "none");
}

alert(who);
Comments