J
Joakim Braun
Why doesn't the below code work?
I'm trying to create a global object and set an event handler to one of its
methods. The function is called, but the object's mTest property is
undefined.
(What I'm trying to do is make a general-purpose solution for the situation
where you have a list box with several associated form elements. When the
element values are changed, you want to update the value of the selected
list option, and when the list selection changes, you want to update the
form elements. So here you could have a "watcher" object that is constructed
with a bunch of element names and handles that stuff without any custom
code.)
Joakim Braun
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function cWatcher(){
this.mTest = "Testing";
this.wire = cWatcher_Wire;
this.changeFunc = cWatcher_Changed;
}
function cWatcher_Wire( inFormName,
inElementName){
document.forms[inFormName].elements[inElementName].onchange =
this.changeFunc;
}
function cWatcher_Changed(){
alert("Changed, test=" + this.mTest);
}
</script>
</head>
<body>
<form id="form1" action="">
<select id="obj1">
<option value="1">Data</option>
<option value="2">More data</option>
</select>
</form>
<script type="text/javascript">
window.gWatcher = new cWatcher();
window.gWatcher.wire("form1","obj1");
</script>
</body>
</html>
I'm trying to create a global object and set an event handler to one of its
methods. The function is called, but the object's mTest property is
undefined.
(What I'm trying to do is make a general-purpose solution for the situation
where you have a list box with several associated form elements. When the
element values are changed, you want to update the value of the selected
list option, and when the list selection changes, you want to update the
form elements. So here you could have a "watcher" object that is constructed
with a bunch of element names and handles that stuff without any custom
code.)
Joakim Braun
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function cWatcher(){
this.mTest = "Testing";
this.wire = cWatcher_Wire;
this.changeFunc = cWatcher_Changed;
}
function cWatcher_Wire( inFormName,
inElementName){
document.forms[inFormName].elements[inElementName].onchange =
this.changeFunc;
}
function cWatcher_Changed(){
alert("Changed, test=" + this.mTest);
}
</script>
</head>
<body>
<form id="form1" action="">
<select id="obj1">
<option value="1">Data</option>
<option value="2">More data</option>
</select>
</form>
<script type="text/javascript">
window.gWatcher = new cWatcher();
window.gWatcher.wire("form1","obj1");
</script>
</body>
</html>