We have a batch of ASPs at work that we affectionately (well, not really) call the "survey generator." They were created by a co-op student a while back, before I even started working here. I've been lucky enough to have avoided it -- until now.
I was trying to apply some simple javascript form validation to it, but for some reason, it kept throwing an error. So, I re-read my references to make sure I was doing this properly. All seemed well.
It turns out, that you can't have a tilde in the name of a form element. Now, I'm not sure who's brilliant idea it was to use tildes in the first place, but it sure puts a wrinkle in my plans. Here's some sample code to show you what's up:
<html>
<head>
<title>Tilde Test</title>
<script language="javascript">
function checker() {
/*
* This next line generates an error due to the
* tilde in the name. Evil.
*
* Line: 15
* Char: 20
* Error: Expected ')'
*/
// if (document.testform.tilde~tilde.checked) {
if (document.testform.elements[0].checked) {
alert('checked!');
}
else {
alert('unchecked!');
}
return false;
}
</script>
</head>
<body>
<form name="testform">
<p><input type="checkbox" name="tilde~tilde" /> Check Me</p>
<p><input type="submit" onclick="return checker();" /></p>
</form>
</body>
</html>
So, now everything has to be referenced by element number (elements[x]
) instead of by name. But, with names like 2~txt~3
, is it really any worse?
Update: Hey... vsergu tells me that
if (document.testform['tilde~tilde'].checked) {
should work. A quick test shows that it does. Thanks! Why don't people use this method? Everywhere i read it's either formname.elementname
or forms[x].elements[y]
.
form_name['weird~element~name']
instead of resorting to numbers. Generally in JavaScript you can refer to properties either the normal way (with a dot) or as elements of an associative array. If the property names aren't usable as identifiers then the associative-array way is the only way.
Now, I'm not sure who's brilliant idea it was to use tildes in the first place, but it sure puts a wrinkle in my plans.
Brilliant pun.