Solved

Can't access style properties of DIVs inside JavaScript loop

Posted on 2000-05-03
3
257 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
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
pauljk earned 150 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

730 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