[thelist] [javascript] getElementById

S.tygian B.lacksmith S.tudios studio at zero.zero.xs2.net
Wed Oct 23 19:19:01 CDT 2002

PB> "My javascript debug window tells me however, that 'ListOption has
no properties', so obviously, the element wasn't able to be 'gotten'.
What is the correct way to use getElementById? There is no duplicate Id
in the page.....:("

If I'm understanding your problem correctly, it's that the node isn't
being assigned correctly *and* you're accessing the wrong value. What
you need to do is access the value of the select element's currently
selected option child.

Here's one way to do it, quickly drafted and tested in IE 6, Opera 6.05
and Netscape 7:

var ListOption;

function init() {
     ListOption = document.getElementById('UserOptions');

function getUOVal() {
     sel = ListOption.selectedIndex;
     ListValue = ListOption.options.item(sel).value;

The reason that I separated the assignment of the node into the init()
function - which would be called onload - is that assigning the node
directly in a var statement doesn't seem to work consistently, if at
all, due to the way that scripts are loaded and parsed. I'm not an
expert on this, but I think that the JS engine may try to assign the
value before the document tree is parsed - and then you get "object
expected" errors, etc.

As far as efficiency goes, I thought that it was more efficient to do it
the way that you are. But there seems to be a difference of opinion on
that. :)

S. G. Kearn

More information about the thelist mailing list