PDA

View Full Version : Object expected



AlfredN
11-21-2006, 03:15 PM
Hi, I've got some javascript code. It works perfectly in Firefox but not in Internet Explorer where I get a "Object Expected" error message on the line with the link. The funcion closes every div with the class name of "class" and opens a div with the id "id". Here is the code:

Function (I've got the function getElementsByClass in my common.js):


function toggle(id,class) {

var a = getElementsByClass(class);
for(var i=0;i<a.length;i++) {
a[i].style.display='none';
}

var b = document.getElementById(id);
if(b.style.display == 'block')
b.style.display = 'none';
else
b.style.display = 'block';
}

And here is the call, I've tried both these versions:


<a href="javascript: toggle('divSubSub7', 'divSubSub');">SubSub7</a>

<a href="#" onclick="toggle('divSubSub7', 'divSubSub'); return false">SubSub7</a>

The thing is that it works perfectly fine in Firefox but not in IE, a little bit strange, or perhaps not ;)

The getElementsByClass function looks like:



function getElementsByClass(searchClass,node,tag) {
var classElements = new Array();
if ( node == null )
node = document;
if ( tag == null )
tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}


When I tried now I got an error on the first line second character and they look like this:


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

Anyone know what I'm doing wrong? Thanks in advance!

Alfred

mastergeek70
11-22-2006, 05:01 PM
I noticed you are calling 'class' as a var name, which I believe is reserved. Try changing that to something different, like var_class and see if it works.

function toggle(id,var_class) {
var a = getElementsByClass(var_class);