Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can't access style properties of DIVs inside JavaScript loop

Posted on 2000-05-03
3
Medium Priority
?
261 Views
Last Modified: 2008-03-10
I can access any DIV tag (to make a style property change) by hard coding the ID of that tag but I want to make the ID dynamic and that's where the trouble starts. My approach is to put the ID values into an array and loop over the array to generate the style switching statements. But I get an error that says style is not an object.

The code is below. The hideAll() function works with the hardcoded ID. You can run it to see the effect I'm trying to achieve. The HideAll() function is the one that uses the dynamic ID but it doesn't work. If you can give me code that works in both IE and Netscape I'd be really happy but I'll take an IE only solution if you can give it.

Thanks,
Rod




<html>
<head>
      <title>Javascript Arrays, DOM and Stylesheets</title>
      
      <script language="JavaScript">
            // an array for the HideAll() function
            aSections=new Array();
            aSections[0]="Section_1";
            aSections[1]="Section_2";
            aSections[2]="Section_3";
      </script>

      <script language="JavaScript">
            function showhide_section(section_to_show) {
              if (section_to_show.style.visibility == "") {
                section_to_show.style.visibility = "hidden";
              } else {
                    hideAll();
                //HideAll(aSections);
                  section_to_show.style.visibility = "";
              }
            }
            
            function hideAll(aSections) {
                  Section_1.style.visibility = "hidden";
                  Section_2.style.visibility = "hidden";
                  Section_3.style.visibility = "hidden";
            }
            
            function HideAll(aMyArray) {
                  for (i=0; i<aMyArray.length; i++) {
                        aMyArray[i].style.visibility = "hidden";
                        //document.write (aMyArray[i] + "<br>"); // debugging
                  }
            }
      </script>
</head>
<body>

<span class=CategoryNavigator style="cursor: hand;" onclick="showhide_section(Section_1)">
Show Marketing
</span>
      &#0149;
<span class=CategoryNavigator style="cursor: hand;" onclick="showhide_section(Section_2)">
Show Sales
</span>
      &#0149;
<span class=CategoryNavigator style="cursor: hand;" onclick="showhide_section(Section_3)">
Show HR
</span>

<br>

<h1 ID=Section_1 STYLE="position:absolute; visibility: hidden;">Marketing</h1>
<h1 ID=Section_2 STYLE="position:absolute; visibility: hidden;">Sales</h1>
<h1 ID=Section_3 STYLE="position:absolute; visibility: hidden;">Human Resources</h1>

</body>
</html>
0
Comment
Question by:rod_nolan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
pauljk earned 600 total points
ID: 2775325

function HideAll(aMyArray) {
for (i=0; i<aMyArray.length; i++) {
eval(aMyArray[i]+'.style.visibility = "hidden";)



Also...  Netscape and IE do not use the same methods to access the style object.  See example.
      if (navigator.appName=="Netscape")
             {
            layerRef="document.layers";
            styleSwitch="";
            }
      else
            {
              layerRef="document.all";
              styleSwitch=".style";
            }      
      if(status==1) eval(layerRef +'["' + menuName + '"]'+styleSwitch+'.visibility="visible"');
            else eval(layerRef +'["' + menuName + '"]'+styleSwitch+'.visibility="hidden"');
0
 
LVL 1

Author Comment

by:rod_nolan
ID: 2777765
Adjusted points from 100 to 150
0
 
LVL 1

Author Comment

by:rod_nolan
ID: 2777766
Thanks Paul, that did it! I knew it was something simple but I've been working on this one for a while so I'm glad to finally see it work. Thanks, again.

Rod
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question