PDA

View Full Version : element id's not recognized by javascript in NN


larkin
03-13-2003, 08:43 PM
I'm having a problem getting my application's javascripts to work in Netscape Navigator even though they work well in IE. I know the javascript is running because I can make an alert, but when I try to validate myform or manipulate elements on my page like opening a hidden div, my javascript console tells me that I have errors that myform and divE are undefined.

Does anyone have an idea why this is not working?

Thanks.

Tommi
03-13-2003, 09:15 PM
try

document.getElementById('id_name')

instead of document.id_name

larkin
03-13-2003, 09:35 PM
Actually, if I put document in front of myform, that works as long as my form has a 'Name'.

For the div, though, it still is not working. It says
document.divE has no properties

or

divE is not defined.

??

cheesebagpipe
03-13-2003, 10:26 PM
All you're doing here is accessing browser objects so they can be scripted. How you get these references depends on 1) what type of objects they are and 2) what browsers you are targeting. document.myform (DOM 0) works because named forms are referenceable as both properties of the document object and properties of the document.forms[] collection (document.forms.myform is also OK). You don't get a 'handle' on a DIV (or many other HTML elements) by referencing it as a document property - it isn't - but by using, as mentioned:

document.getElementById('element_id');

...which is a method - i.e., a function (DOM 1+).

IE exposes element references everywhere, don't use it as a benchmark. btw, 'Navigator' was dropped v.6.

larkin
03-14-2003, 04:25 PM
That does seem to work for NN 6 and 7. Thanks.

Any clue why window.event.keyCode does not work though? I am getting an error that 'window.event has no properties'.

beetle
03-14-2003, 04:36 PM
event.which

Also, if you are doing stuff cross browser, I suggest abstracting the element handle getting process with a function like this
functiong getNode( id )
{
return ( document.layers ) ? document.layers : ( document.getElementById || document.all )( id );
}[I]The above code snippert originally brought to you by: jkd :thumbsup:

larkin
03-14-2003, 05:34 PM
Can you change the keyCode or in this case event.which? Like
if(event.which ==13) event.which = 9;


Also, the getNode function looks pretty good. If I were using a div that had style="display:none;" I would have to check:

if(getNode("divE").style)
{
getNode("divE").style.display = '';
}
else
{
getNode("divE").visibility = "visible";
}

Wouldn't I?

beetle
03-14-2003, 06:19 PM
Yes, but better to use a reference instead of re-calling the function each time
var o = getNode( 'divE' );
if( o.style )
{
o.style.display = '';
}
else
{
o.visibility = "visible";
}event.which is probably read-only.

larkin
03-14-2003, 06:36 PM
Thank you very much for your help.