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
Solved

Javascript IE error

Posted on 2007-03-26
8
260 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 look for a specific file type in a local or remote server directory using PHP.

838 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