I see what I did wrong. I iterated over Thing1.Type, but assigned the results to Thing1:
Code:
for(var p in Thing1.Type){
Thing1[p] = p === newType;
}
should have been:
Code:
for(var p in Thing1.Type){
Thing1.Type[p] = p === newType;
}
That should fix it.
Also, though I don't think this is an issue, just something to know as it saves time, the prototype constructor returns the instance implicitly, so doesn't have to explicitly. In other words, this:
Code:
function Thing() {
this.Type = {
'OneType' : false,
'OtherType' : false
};
return this;
}
Can be just:
Code:
function Thing() {
this.Type = {
'OneType' : false,
'OtherType' : false
};
}
Just thought I'd add a possible way to deal with all this:
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<label>Choose Thing</label>
<select size="1" name="thing_type" id="thing_type">
<option value="OneType">Type 1</option>
<option value="OtherType">Type 2</option>
</select>
<script type="text/javascript">
function Thing() {
this.Type = {
'OneType' : false,
'OtherType' : false
};
}
var Thing1 = new Thing();
var f = document.getElementById("thing_type");
function setBoleanVals(){
var newType = this.value;
for(var p in Thing1.Type){
Thing1.Type[p] = p === newType;
}
if(window.console){console.log(Thing1);} /* diagnostic */
}
f.addEventListener('change', setBoleanVals, false);
setBoleanVals.apply(f);
</script>
</body>
</html>
Or even:
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<label>Choose Thing</label>
<select size="1" name="thing_type" id="thing_type">
<option value="OneType">Type 1</option>
<option value="OtherType">Type 2</option>
</select>
<script type="text/javascript">
function Thing() {
}
Thing.prototype = {
Type: {'OneType' : false, 'OtherType' : false}
};
var Thing1 = new Thing();
var f = document.getElementById("thing_type");
function setBoleanVals(){
var newType = this.value;
for(var p in Thing1.Type){
Thing1.Type[p] = p === newType;
}
if(window.console){console.log(Thing1);} /* diagnostic */
}
f.addEventListener('change', setBoleanVals, false);
setBoleanVals.apply(f);
</script>
</body>
</html>
Here's another way you might find interesting:
Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<label>Choose Thing</label>
<select size="1" name="thing_type" id="thing_type">
<option value="OneType">Type 1</option>
<option value="OtherType">Type 2</option>
</select>
<script type="text/javascript">
function Thing(select) {
this.select = select;
this.Type = {};
this.setBooleans = function(){
for(var p in this.Type){
this.Type[p] = p === this.select.value;
}
if(window.console){console.log(this);} /* diagnostic */
}
var o = select.options, c = -1;
while(o[++c]){this.Type[o[c].value] = o[c].value === select.value;}
}
var Thing1 = new Thing(document.getElementById("thing_type"));
if(window.console){console.log(Thing1);} /* diagnostic */
Thing1.select.addEventListener('change', function(){Thing1.setBooleans();}, false);
</script>
</body>
</html>
Bookmarks