Solved

Problem with getElementsById

Posted on 2004-03-23
6
21,348 Views
Last Modified: 2012-05-04
I have the following javascript:

<script type="text/javascript">
    <!--//<![CDATA[
    var current = 1;
    var offClass = new Array('about','products','services','contact');
 
     function clearMenuCSS()
     {
       for(i=0; i < 4; i++)
       {
         if(_itemRef != i)
         {
           document.getElementById(i).className = offClass[i];
         }
       }
     }
     //]]>-->
    </script>

What I want it to do is change the class of <div id="1...4"></div> unless the div id is the same as _itemRef, however there seems to be a problem with the document.getElementById(i).className = offClass[i]; line as it says it has no properties. How do I access the divs with id 1,2,3,4 using a for loop?

Thanks
0
Comment
Question by:jstrideuk
  • 2
6 Comments
 
LVL 7

Accepted Solution

by:
searlas earned 125 total points
ID: 10658298
If your id is starting with a number it is invalid.  Try chaning them to "d1", "d2", "d3", "d4"...
Then:
function clearMenuCSS()
     {
       var divIds = [ "d1", "d2", "d3", "d4" ];
       for(var i=0; i < divIds.length; i++)
       {
         if(_itemRef != divIds[i])
         {
           document.getElementById(divIds[i]).className = offClass[i];
         }
       }
     }
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10658665
The problem could probally be that id nameing conventions do not start with numbers
You need to have (as stated above) any letter a-z or A-Z at the beginning and then use
what ever numbers you choose.

again start with letter then number(S)
0
 
LVL 16

Expert Comment

by:jaysolomon
ID: 10658671
ie

<div id="d1">...... as  searlas stated
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 125 total points
ID: 10659413
Check also this:


<script type="text/javascript">
   <!--//<![CDATA[
   var current = 1;
   var offClass = new Array('about','products','services','contact');
 
    function clearMenuCSS()
    {
      divs = document.getElementsByTagName('div')
      for(i=0; i < 4; i++)
      {
        if(_itemRef != i)
        {
          divs[i].className = offClass[i];
        }
      }
    }
    //]]>-->
   </script>



0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

789 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