Solved

Can't access style properties of DIVs inside JavaScript loop

Posted on 2000-05-03
3
253 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Timerpicker in javascript 4 34
Angular 2 - Issue in page Layout 2 46
Jquey and footrable 2 34
what is the difference between call and apply 1 25
This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now