Solved

Can't access style properties of DIVs inside JavaScript loop

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

759 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

18 Experts available now in Live!

Get 1:1 Help Now