Solved

Problem with getElementsById

Posted on 2004-03-23
6
21,337 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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'…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

773 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