Hello all.
I am going insane trying to figure out why this is not working. I am creating a checkbox dynamically with JS to enable/disable a text field (also created dynamically).
Code is pretty simple:
var text = $(document.createElement("input"));
text.type = "text";
container.appendChild(text);
...
var check = $(document.createElement("input"));
check.type = "checkbox";
check.onclick = function(event) {
if (this.checked) text.enable();
else text.disable();
};
container.appendChild(check);
I've tried putting the event handler after container.appendChild(check);
, but that yields the same result.
There are no JS errors, and results are the same in Chrome, FF3.6, and IE7.
Can anyone explain why this isn't working? Any fixes would be appreciated.
Strangely (because theoretically it should be the same as above), adding the following (after container.appendChild(check);
) works (checkbox is first on page):
$$("input[type=checkbox]")[0].onclick = function(event) {
if (this.checked) text.enable();
else text.disable();
};
I don't really like this, as it seems "dirty", and the checkbox may not be the first on the page later on...
BTW, $ and $$ are from Prototype.
Thanks in advance for any help.