Results 1 to 2 of 2

Thread: Object expected

  1. #1
    Join Date
    Nov 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Object expected

    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):

    Code:
    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:

    Code:
    <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:

    Code:
    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:

    Code:
    <!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

  2. #2
    Join Date
    Nov 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Object expected

    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);

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •