...

View Full Version : Object error - IE only



jonathansf
05-09-2006, 07:53 PM
Hi,

I'm getting this error: "error: object required". This only happens in IE. Code works fine in FF w/ no errors. The error occurs when I click on one of the links, which are used to hide/show elements. Here is the Javascript:

<script language="javascript" type="text/javascript">

function startList() {
if (!document.getElementById) return false;
if (!document.getElementById("campus_list")) return false;
var campuslist = document.getElementById("campus_list");
var links = campuslist.getElementsByTagName("a");
for(var i=0; i<links.length; i++) {
links[i].onclick = function() {
var campus = this.getAttribute("class");
displayCampus(campus);
}
}
}

function displayCampus (campus) {
if (!document.getElementById("campus_details")) return false;
var campusdetails = document.getElementById("campus_details");
var campuslist = campusdetails.getElementsByTagName("ul");
for(var i=0; i<campuslist.length; i++) {
campuslist[i].style.display = "none";
}
var whichcampus = campus+"_campus";
var showcampus = document.getElementById(whichcampus);
showcampus.style.display = "block";
}

window.onload=startList;

</script>

Any ideas?

Thanks in advance,
Jonathansf

SpirtOfGrandeur
05-09-2006, 08:08 PM
Full code please in [code] brackets!

jonathansf
05-09-2006, 08:15 PM
Full code is posted here:

http://www.extremecroquet.com/Test/

Thanks.

jonathansf
05-09-2006, 08:54 PM
I found the answer in the archive on these boards.

Posted by liorean:

ie doesn't handle setAttribute and getAttribute correctly. It treats elm.setAttribute('attribute',value) as elm.attribute=value. Which means:

1. It breaks if you try to set attributes that are JavaScript keywords or reserved keywords such as for, class etc.
2. It's case sensitive for HTML, which it shouldn't be.
3. It doesn't handle attributes containing certain characters.

Using className you avoid this problem altogether.

So, in my case:

var campus = this.getAttribute("class");

becomes

var campus = this.className;

Cheers,
JP

liorean
05-09-2006, 09:27 PM
Nice to be of assistance :)

Kor
05-10-2006, 10:14 AM
You may use getAttributeNode() as well. Weird enough (but typically for the IE inconsistency) this time "class" is accepted by IE.

var campus = this.getAttributeNode("class").nodeValue;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum