Solved

Javascript IE error

Posted on 2007-03-26
8
261 Views
Last Modified: 2012-05-05
Hi,


I have a function that toggles show or hide of particular divs. There are many that are named the same hence why i use a for loop.
unfortunately IE throws an error message and the function doesnt work . In firefox it is fine.
Any help much appreciated.

here is my function:

function toggle(id) {
                  x = document.getElementsByName(id);
                  for (key in x) {
                   if (x[key].style.display == 'block') {
                        x[key].style.display = 'none';
                   } else {
                        x[key].style.display = 'block';
                   }
                  }
            }

Thanks
0
Comment
Question by:coolispaul
[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
  • 3
  • 2
8 Comments
 
LVL 7

Expert Comment

by:RealSnaD
ID: 18794394
Try this:

function toggle(id) {
   x = document.getElementById(id);
   for (key in x) {
      if (x[key].style.display == 'block') {
         x[key].style.display = 'none';
      } else {
         x[key].style.display = 'block';
      }
   }
}
0
 
LVL 7

Expert Comment

by:RealSnaD
ID: 18794411
or if you need to identify DIVs by name, change

x = document.getElementById(id);

to

x = document.getElementByName(id);

(Not document.getElementsByName(id) !)
0
 

Author Comment

by:coolispaul
ID: 18798431
document.getElementByName(id) doesnt work.

firefox does nothing and IE says "object not supported by this property or method"

Any other ideas?

Thanks
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 7

Accepted Solution

by:
RealSnaD earned 250 total points
ID: 18800919
Well this code will work if you identify all DIVs using id ( <div id="div1"> ):

function toggle(id) {
   x = document.getElementById(id);
   for (key in x) {
      if (x[key].style.display == 'block') {
         x[key].style.display = 'none';
      } else {
         x[key].style.display = 'block';
      }
   }
}
0
 

Author Comment

by:coolispaul
ID: 18835397
it only works with name. i think because id is unique and only grabs one element.

However, im still having a problem with this in IE.
The error i get is : 'style.display' is null or not an object

0
 
LVL 1

Assisted Solution

by:Humbir
Humbir earned 250 total points
ID: 18971680
Hi coolispaul,

getElementsByName method works only for input elements in IE.
So we can slightly tweak the code to make it IE compatible:
<script language="javascript">
<!--
function toggle(id)
{
 var x = document.getElementsByTagName('div');
 for (key in x)
 {
 if (x[key].name == id)
  {
  if (x[key].style.display == 'none')
   {
   x[key].style.display = 'block';
   }
  else
   {
   x[key].style.display = 'none';
   }
  }
 }
 return false;
}
//-->
</script>

Please let me know if you are still facing problem with IE.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 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