Why javascript strings not equal?

S

seanbrasher

I have the following code:
---------------------------------------------
<html>
<head>
<script language="javascript">
function readHiddenField()
{
var string1 = 'hidden_field_l00';
var string2 = 'hidden_field_' + '100';

alert(string1 == string2);

var sampleRow = document.getElementById(string1);
alert(sampleRow.value);

var hiddenRow = document.getElementById(string2);
alert(hiddenRow.value);

}
</script>
</head>
<body>
<form>

<input type="hidden" id="hidden_field_l00" value="hello" />

<a href="#" onclick="readHiddenField();">Click Me</a>

</form>
</body>
</html>
---------------------------------------------
To me, this should return three alert boxes that say "true", "hello",
and "hello". Instead, it returns "false", "hello", and then I get an
error for null reference because getElementById did not find the
element on the last call. What is it about the string concatenation
that makes it think these strings are not equivalent, and how can I
prevent this?

I know that if you replace '100' with 'X' in the field name and in the
script, then it works as expected, so this has to be somehow based on
number/string conversion, but there should not be any conversion going
on in the first place.

(Also, this is in IE 6, which is the only browser I need this to work
in, though I am curious if this is cross-browser behavior. )
 
M

marss

<input type="hidden" id="hidden_field_l00" value="hello" />

There is a simple mistake. Your input id is "hidden_field_l00" not
"hidden_field_100" (in uppercase L instead of 1)
 
S

seanbrasher

Thanks :)

I now officially hate Courier New...

Just switched to Andale Mono font, where you can actually see the
difference between an l and a 1.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top