Reputation: 550
The following code snippet demonstrates my problem. When I test a string that contains the "&" the result is always false.
How should this be handled? JavaScript solution only please.
var PROJECT = "This & Somthing Else";
var PROJECT2 = "This and Somthing Else";
function testValues()
{
if (PROJECT == document.getElementById("curPrjct").innerHTML)
{
alert("The Same");
}else{
alert("Not The Same");
}
}
function testValues2()
{
if (PROJECT2 == document.getElementById("curPrjct2").innerHTML)
{
alert("The Same");
}else{
alert("Not The Same");
}
}
<!doctype html>
<html lang="en">
<head>
</head>
<body>
<span id="curPrjct">This & Somthing Else</span> <br>
<input type="button" value="Go This will fail" onclick="testValues()"><br><br>
<span id="curPrjct2">This and Somthing Else</span> <br>
<input type="button" value="Go This will work" onclick="testValues2()">
</body>
</html>
Upvotes: 2
Views: 61
Reputation: 943591
The problem with trying to look at the HTML is that when it is converted to a DOM and then back to HTML, it will be normalised.
&
will be converted to &
.
Compare the textContent
instead of the HTML.
var PROJECT = "This & Somthing Else";
var PROJECT2 = "This and Somthing Else";
function testValues()
{
if (PROJECT == document.getElementById("curPrjct").textContent)
{
alert("The Same");
}else{
alert("Not The Same");
}
}
function testValues2()
{
if (PROJECT2 == document.getElementById("curPrjct2").textContent)
{
alert("The Same");
}else{
alert("Not The Same");
}
}
<!doctype html>
<html lang="en">
<head>
</head>
<body>
<span id="curPrjct">This & Somthing Else</span> <br>
<input type="button" value="Now this will work" onclick="testValues()"><br><br>
<span id="curPrjct2">This and Somthing Else</span> <br>
<input type="button" value="Go This will work" onclick="testValues2()">
</body>
</html>
Upvotes: 2