Solved

Javascript IE error

Posted on 2007-03-26
8
267 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
Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

 
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

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 strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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