/    Sign up×
Community /Pin to ProfileBookmark

problem when comparing strings containing "&"

hi.
i am using javascript to compare two strings. it works well with normal strings. but when the strings contain “&” (e.g. & #1234 ; ), it does not work well.

code:
q = document.getElementById(“q”).value;
a = document.getElementById(“a”).value;
if (q == a)
document.formName.all(“check”).innerText = “correct”;
else
document.formName.all(“check”).innerText = “wrong”;

anyone have any idea to solve this problem.. thanks.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@FangNov 28.2005 — Give a complete code example

btw best not use [I]all("check").innerText[/I], it only works in IE
Copy linkTweet thisAlerts:
@xghostauthorNov 29.2005 — code:

function autoGrade() {

q = document.getElementById("q").value;

a = document.getElementById("a").value;

if (q == a)

document.formName.all("check").innerText = "correct";

else

document.formName.all("check").innerText = "wrong";

}

<form name="formName">

<input type="text" id="q" name="q" value="" />

<input type="hidden" id="a" name="a" value="& #1234;" />
<span id="check"></span>
<input type="button" value="Submit" onClick="autoGrade()" />
</form>



if dont use use all("check").innerText, then what is the alternative?

thanks.
Copy linkTweet thisAlerts:
@FangNov 29.2005 — The example given works for me.
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;q &amp;amp; a&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
function autoGrade(objID) {
var q = document.getElementById("q").value;
var a = document.getElementById("a").value;
var answer=(q == a)? "correct" : "wrong";
var obj=document.getElementById(objID);
if(obj.firstChild!=null) { // obj already contains text
obj.firstChild.data=answer;
}
else { // obj contains no text
var tn=document.createTextNode(answer);
obj.appendChild(tn);
}
}
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;form name="formName" action="#"&gt;
&lt;div&gt;
&lt;input type="text" id="q" name="q" value=""&gt;
&lt;input type="hidden" id="a" name="a" value="&amp;amp; #1234;"&gt;
&lt;span id="check"&gt;&lt;/span&gt;
&lt;input type="button" value="Submit" onclick="autoGrade('check')"&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@xghostauthorDec 02.2005 — hey. thanks a lot Fang.
×

Success!

Help @xghost spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...