Solved

Problem with getElementsById

Posted on 2004-03-23
6
21,358 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make google charts responsive 4 53
Any Way to rotate banner ads from Commission Junction? 2 38
Compute age Html 2 27
Session on Html 8 42
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

726 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