Forum Moderators: open

Message Too Old, No Replies

can't hide div is NS

javascript to hide NS div works in IE

         

jerryb

4:52 am on Nov 29, 2004 (gmt 0)

10+ Year Member



In subject hide should read show

The code below works in IE6 (hurrah and wonders).
According to all I have read it should also work in NS6 or 7/Firefox - but no it doesn't.

Not supporting older browsers on this site.

Subbing document.GetElementById(me).style for me.style
works in IE and not in NS etc.

Avoiding visibility property due to it consuming inline space. besides this should work for either-right?

What am I not seeing?

Relevant CSS

#newslist { font-size: 12px; background-color: #ffffff; display: none; position:relative; z-index: 6; left: 50px; width: 150px; border: solid 2px #b54905 }

Relevant js

function chgdsplay(me)
{
if (me.style.display=="none"){
me.style.display="block";}
else {
me.style.display="none";}
}

Relevant Html

<div onclick="chgdsplay(newslist)" id="newsbox">
News
</div><!-- end of newsbox -->
<div onmouseout="chgdsplay(newslist)" id="newslist">
<a id="link1" href="http://www.link1.com" onmouseover="chgdsplay(newslist)">link1</a><br>
<div onmouseout="chgdsplay(newslist)" id="newslist">
<a id="link2" href="http://www.link2.com" onmouseover="chgdsplay(newslist)">link2</a><br>
</div><!-- end of newslist-->

Thanks a almost newbie

orion_rus

11:45 am on Nov 29, 2004 (gmt 0)

10+ Year Member



It's seems like u call a function with the name of object. But style can be applied to object only. What's why u need makes before document.getElementById(id) or document.all['id'].

jerryb

6:32 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



Thanks but..

The ObjID.style shortcut method actually works in IE6/7 (strict mode)as does the Document.GetElementByID(ObjID).style method.

But neither works in NS, which is what I am looking to solve. I have this code listed as cross browser but it is possible I am wrong. If so How would you code for NS?

orion_rus

6:35 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



I think NS working with css style but NS before 6 version can't do getElementById u should use compotable mode like document.all if u need i can parse a compotable function for u

Bernard Marx

9:02 pm on Nov 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A case of case?

document.getElementById(ObjID)

(It ain't VBScript y'know!)

jerryb

2:32 am on Nov 30, 2004 (gmt 0)

10+ Year Member



Thanks Guys

You are so right! - it ain't VB - and Capping everyword as a bad habit killed the script. Did it right for IE and capped the Get part for NS. Best I can figure is the ObjId.style short cut only works in IE.
Have to use the long form i guess.
Again thanks

Bernard Marx

10:04 am on Nov 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's longer, jerry, but you only need to use it once:

function chgdsplay(me)
{
var style = document.getElementById(me)
style.display = style.display == "none"? "block" : "none";
}

jerryb

10:09 pm on Nov 30, 2004 (gmt 0)

10+ Year Member



make that Thanks twice

orion_rus

8:23 am on Dec 1, 2004 (gmt 0)

10+ Year Member



I think style.display wouldn't work because of style is obj, and reserved word. better make
obj1=document.getElementById(me);
obj1.style.display....

Bernard Marx

10:11 am on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Style is not a reserved word. It's simply a property name. Using a local variable of the same name will not conflict. I personally find it easier than making up prefixes like 'oStyle'.

..but I did make a mistake in the code. It's supposed to reference the style object directly (why not go straight for the object we want). I forgot to...

[pre]
function chgdsplay(me)
{
var style = document.getElementById(me)[red].style[/red];
style.display = style.display == "none"? "block" : "none";
}
[/pre]

orion_rus

4:46 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



bernard i think u right what it's not a reserved word. But i prefer not to use such words, because i don't know construction of all browsers. May be in some browsers it's a reserved word))

Bernard Marx

5:28 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think you should worry mate. The only 'real' reserved words are the ones built into Javascript/JScript - words like for, in, while, function, class...

The only time you need worry about browser implementations is when choosing global variable names, because you run the risk of conflict with window methods and properties, some of which are added by browser makers (window.opera for instance).

The variable is question here is local, so there are no worries.