Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to reference an id in IE and Netscape 2

Status
Not open for further replies.

tnsbuff

Technical User
Jan 23, 2002
216
US
Hi,

It seems that I can reference a div id using:

document.all('idname')

but I can't seem to get Netscape to work. I tried using:

document.getElementByID('idname')

but it doesn't seem to work.

Thanks for any tips,

 
Hmmmm, maybe it's the way I'm referencing it. Here's what I have:
Code:
<script>
if(document.all){
banner = document.all('banner');
}

if(document.getElementByID){
banner = document.getElementByID('banner');
}
</script>

and then for a button I have:

<form>
<input name="close" value="Close Banner" type="button" onClick="banner.style.display='none'">
</form>
 
Like tgreer said, you can't use [tt]document.getElementByID[/tt], you need to use [tt]document.getElementByI[red]d[/red][/tt]

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Sorry, I missed the part about "capitalization" and just checked the "spelling".

It works now.

Thanks to both of you!
 
Actually, one more question. It works when I use a Flash file and set its id to banner, but if I switch out the Flash file and just use an h1 tag or something (with the id set within that tag), it doesn't work. It also doesn't work when I just set the div's id to 'banner'.

Why would that be?

Thanks,
 
Do you have multiple elements with id's of 'banner'? Also, what errors (if any) are you getting?

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
I don't think so. This works:

Code:
<html><head>
<style type="text/css">
#banner {display: inline; text-align: center}
</style>
<script>
if(document.all){
banner = document.all('banner');
}
if(document.getElementbyId('banner'){
banner = document.getElementById('banner');
}
</script>
</head>

<body bgcolor="#FF6600">
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="[URL unfurl="true"]http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,79,0"[/URL]
  id="banner" width="750" height="125">
  <param name="movie" value="Movie1.swf">
  <param name="bgcolor" value="#FF6600">
    <embed type="application/x-shockwave-flash"   pluginspage="[URL unfurl="true"]http://www.macromedia.com/go/getflashplayer"[/URL]
   width="750" height="125"
   name="Movie1" src="Movie1.swf"
   bgcolor="#FF6600"></embed></object>

<form><input name="close_banner" value="Close Banner" type="button" onClick="banner.style.display='none'"></form>

</body>
</html>

This doesn't work:

Code:
<html><head>
<style type="text/css">
#banner {display: inline; text-align: center}
</style>
<script>
if(document.all){
banner = document.all('banner');
}
if(document.getElementbyId){
banner = document.getElementById('banner');
}

</script>
</head>
<body bgcolor="#FF6600">

<div id="banner">This is a test.</div>

<form><input name="close_banner" value="Close Banner" type="button" onClick="banner.style.display='none'"></form>

</body>
</html>
 
Oh, and I'm getting the "object expected" error in IE on the line that contains my form button.

Thanks for your help.
 
. . . and to complicate matters, the second one does work in Mozilla Firefox (but not in Netscape7 or IE6). :-\
 
Has something to do with you globally referencing "banner", since "banner" is both a JS variable and an element id, some browsers get confused.



Code:
<html><head>
<style type="text/css">
#banner {display: inline; text-align: center}
</style>
<script>

function hide(id) {
    document.getElementById(id).style.display = 'none';
}

</script>
</head>
<body bgcolor="#FF6600">

<div id="banner">This is a test.</div>

<form><input name="close_banner" value="Close Banner" type="button" onClick="hide('banner');"></form>

</body>
</html>

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
>>Has something to do with you globally referencing "banner", since "banner" is both a JS variable and an element id, some browsers get confused.<<

But then why does it work when the id is a flash object, but not when it's a div?
 
Not sure. It worked fine with my code.

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
>>Not sure. It worked fine with my code.<<

What code do you mean?

I tried changing the variable name to "top", and referencing it in the form button, but it still doesn't work.
 
The code I just pasted.

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Okay, thanks. It seems to want it in a function, although it doesn't require it when the id pertains to a flash file.

I'll use this method.

Thanks again!
 
No prob.

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Ahh, very good point. I once was developing a site and had named a filler image "banner-ad.gif" and it would not show up in one of the browsers. It was a nightmare until intuition set in and I renamed the image.

*cLFlaVA
----------------------------
"Holy crip he's a crapple!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top