[JS] Divs not showing

He has: 296 posts

Joined: May 2002

[EDIT]Looking at it, when you click the Update one it works fine. But Features does not...[/EDIT]

I have a javascript that displays a set of Divs if they are hidden and hides them if they are visible. Unfortunatley, when the javascript is run the divs are moved alright but they are invisible. To see what I mean, go to http://www.logsdon.org/xjamesx/at/56k and click on either Features or Updates (with the Plus signs next to them). See how blank space appears but nothing else? That's supposed to be three seperate Divs nested in a container Div.

Javascript:

    function ShowHide(id) {
      if (document.all(id).style.position == "absolute") {
        document.images("IMG_"+id).src = "../images/minus1.gif";
        document.all(id).style.position = "relative";
        document.all(id).style.top = "0px";
        document.all(id).style.display = "";
      } else {
        document.all(id).style.position = "absolute";
        document.all(id).style.top = "-1000px";
        document.all(id).style.display = "none";
        document.images("IMG_"+id).src = "../images/plus1.gif";
      }
    }
'

This is probably not the best code right now, but I do my coding weird (at least I think I do). I get it working in the browser I use the most (unfortunatley), IE6. Then I get it to work in other browsers. Its just easier for me to do since my dad has it set so I can't install anything Sad.

Any help you could provide would be GREATLY appreciated.

Thanks,
necrotic

P.S. The vB3 is nice Smiling

[James Logsdon]

He has: 1,380 posts

Joined: Feb 2002

i don't see anything wrong (WinXP, IE)

He has: 296 posts

Joined: May 2002

That's what happens. What version of IE?

druagord's picture

He has: 335 posts

Joined: May 2003

you should use getelementbyid for that kind of things (it would work in IE and other browser) try to use visibility instead of display parameters are visible or hidden

IF , ELSE , WHILE isn't that what life is all about

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

<?php
   
&lt;script type=\"text/javascript\"&gt;
    <!--
    function ShowHide(id) {
      if (document.getElementById(id).style.position == \"absolute\") {
        document.getElementById(\"IMG_\"+id).style.src = \"images/minus1.gif\";
        document.getElementById(id).style.position = \"relative\";
        document.getElementById(id).style.top = \"0px\";
        document.getElementById(id).style.display = \"block\";
      } else {
        document.getElementById(id).style.position = \"absolute\";
        document.getElementById(id).style.top = \"-1000px\";
        document.getElementById(id).style.display = \"none\";
        document.getElementById(\"IMG_\"+id).style.src = \"images/plus1.gif\";
      }
    }
    -->
    &lt;/script&gt;
?>

Works in Mozilla Firebird. To make life a little easier, you might want to code to xhtml strict or transitional. Helps with the DOM sometimes. Also use & copy ; instead of the copyright character at the bottom.

Oh, and change paths to suit.

Smiling

He has: 296 posts

Joined: May 2002

Abhishek Reddy wrote:

<?php
   
&lt;script type=\"text/javascript\"&gt;
    <!--
    function ShowHide(id) {
      if (document.getElementById(id).style.position == \"absolute\") {
        document.getElementById(\"IMG_\"+id).style.src = \"images/minus1.gif\";
        document.getElementById(id).style.position = \"relative\";
        document.getElementById(id).style.top = \"0px\";
        document.getElementById(id).style.display = \"block\";
      } else {
        document.getElementById(id).style.position = \"absolute\";
        document.getElementById(id).style.top = \"-1000px\";
        document.getElementById(id).style.display = \"none\";
        document.getElementById(\"IMG_\"+id).style.src = \"images/plus1.gif\";
      }
    }
    -->
    &lt;/script&gt;
?>

Works in Mozilla Firebird. To make life a little easier, you might want to code to xhtml strict or transitional. Helps with the DOM sometimes. Also use & copy ; instead of the copyright character at the bottom.

Oh, and change paths to suit.

Smiling

Already is & copy ;.

I'll try that code, thanks.

[James Logsdon]

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

It's already & copy ;? That means my editor is parsing HTML character code. Or maybe it was Mozilla while saving the page. Sorry about that. Smiling

Tell me if that code works right in IE. I have no idea what IE's support of DOM/ECMA standards is like anymore. Sad

He has: 296 posts

Joined: May 2002

It works in IE, but the same problem exists. I'd look at it closer, but I have to do a project for school now. It's a large one, too. Doing a pixel art of a Manor from Medieval Times. Fun stuff, but hard. Thanks for the help.

[James Logsdon]

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

It works in Mozilla Firebird 0.7 on Linux. Only think is that the images don't change, but that's because you haven't altered the paths for your own filing structure. What browers does it fail in?

Good luck on that project, sounds like a lot of work. Smiling

He has: 296 posts

Joined: May 2002

Actually, I did change the path to ../images/. Strange stuff.

In opera 6 it doesn't work at all. IE the same problem exists. Mozilla it works fine. What I'll probably end up doing is having the javascript only on Mozilla. For IE and Opera it'll just be automatically opened (PHP most likely, I'm better at that).

Thanks for the help Abhishek!

[James Logsdon]

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Check this out.

He has: 296 posts

Joined: May 2002

Wow, that's great! Thanks so much Abhi!

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

No problems. It's pretty much what you had before, but without the frills. Sometimes we complicate things too much. Took me a while to realise that I was overlooking the positioning bit. Smiling

He has: 296 posts

Joined: May 2002

I forgot that display: none made it actually display nothing. I'm used to visibility: hidden which just hides it and still keeps the spacing. Thanks again.

[James Logsdon]

Vincent Puglia's picture

They have: 634 posts

Joined: Dec 1999

Hi,

if you send a boolean along with the id, you can simplify the code even further:

function ShowHide(divID, showIt )
{
document.getElementById("IMG_"+divID).style.src = (showIt) ? "images/minus1.gif" : "images/plus1.gif";
document.getElementById(divID).style.display = (showIt) ? "block" : "none";
}

Vinny

Where the world once stood
the blades of grass cut me still

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.