Solved

Javascript IE error

Posted on 2007-03-26
8
258 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to count occurrences of each item in an array.

813 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

17 Experts available now in Live!

Get 1:1 Help Now