PDA

View Full Version : getelementbyid help


homerUK
09-22-2004, 02:43 PM
hey again!

I have this code which works:

keyCode = editor1.event.keyCode;
alert(keyCode);

but when I change it to use the getElementById method, it doesnt work...

theid = "editor1";
alert(document.getElementById(theid).event.keyCode);

it says document.getElementById(...) is null or not an object...

even

alert(document.getElementById("editor1").event.keyCode); doesnt work.

can someone please shed some light on what I might be doing wrong!? thanks!

JPM
09-22-2004, 03:14 PM
not sure, but you could try storing document.getElementById(theid).event.keyCode
in a variable first...

homerUK
09-22-2004, 03:16 PM
just tried

theid = "editor1";
test = document.getElementById(theid).event.keyCode;
alert(test);

and still get the same error.
when I try

theid = "editor1";
test = document.getElementById(theid).style;
alert(test);

that works - its just like the event.keyCode isnt working properly - possibly I've used the wrong syntax?

jbot
09-22-2004, 03:44 PM
all i can think of is that event.keyCode is a property of the window object, not of document.getElementById("editor1"). of course, in IE-world editor1 can be shorthand for document.getElementById("editor1"), so you'd expect it to work, wouldn't you. well, not necessarily, because using the shorthand method probably refers to the event within the global namespace, not within that of the element.

that's all i can think of, and it's probably wrong, but that's my tuppence worth anyway. hope it helps :D