Solved

Problem with getElementsById

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

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
Download a website to hdd 2 52
Split in Javascript 5 31
Use a select control to call a javascript function 5 31
how can i count words? 2 21
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…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now