Solved

Problem with getElementsById

Posted on 2004-03-23
6
21,312 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
Comment Utility
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
Comment Utility
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
Comment Utility
ie

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

Assisted Solution

by:Zvonko
Zvonko earned 125 total points
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

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…
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
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…

772 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

9 Experts available now in Live!

Get 1:1 Help Now