...

View Full Version : show/hide div jscript not working on IE



sircheel
06-16-2009, 11:29 AM
Hi, i have this jscript code and I want to show the first div onLoad. Trying to figure this out, what I did was:

<body onload="showdiv(0);">

Running on firefox works fine, the first div is showing onload BUT testing on IE doesn't work at all. It displays an error:

document.getElementById()is null or not an object

here's the jscript i'm using:
<script type="text/javascript">

var c;
var k=0;
var j=0;
var ary=[];
var divs=document.getElementsByTagName('div');
var lnks=document.getElementsByTagName('a');
var splt;

function init() {
for(c=0;c<divs.length;c++) {
if(divs[c].className=='show') {
divs[c].className='hide';
divs[c].id='d'+k++;
}
}
for(c=0;c<lnks.length;c++) {
if(lnks[c].className=='hide') {
ary[c]=true;
lnks[c].className='hs';
lnks[c].id='a'+j++;
lnks[c].onclick=function() {
splt=this.id.split('a')[1];
if(ary[splt]==true) {
this.firstChild.nodeValue='hide '+(parseFloat(splt)+1);
ary[splt]=false;
showdiv(splt);
return false;
}
else {
this.firstChild.nodeValue='show '+(parseFloat(splt)+1);
ary[splt]=true;

document.getElementById('d'+splt).className='hide';
return false;
}
}
}
}
}


function showdiv(num) {
for(c=0;c<j;c++){
document.getElementById('a'+c).firstChild.nodeValue='show '+(c+1);
document.getElementById('a'+num).firstChild.nodeValue='hide '+(parseFloat(num)+1);
ary[c]=true;
ary[num]=false;
}
for(c=0;c<divs.length;c++) {
if(divs[c].className=='show') {
divs[c].className='hide';
}
}
document.getElementById('d'+num).className='show';<--- this is the line error
}

if(window.addEventListener){
window.addEventListener('load',init,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',init);
}
}

</script>

How will i solve this issue? So frustrating...:confused:
Hope someone can take a peek on my codes.

abduraooft
06-16-2009, 02:27 PM
Please post your complete code including html(with DOCTYPE)

btw, use
][/COLOR] tags to wrap your code while posting here. You may edit your above post too.

vwphillips
06-16-2009, 02:41 PM
<script type="text/javascript">

var ary=[];
var divs;

function init() {
divs=document.getElementsByTagName('div');
var lnks=document.getElementsByTagName('a');
for(var c=0;c<divs.length;c++) {
if(divs[c].className=='show') {
divs[c].className='hide';
divs[c].id='d'+c;
}
}
for(c=0;c<lnks.length;c++) {
if(lnks[c].className=='hide') {
ary[c]=true;
lnks[c].className='hs';
lnks[c].id='a'+c;
lnks[c].onclick=function() {
var splt=this.id.split('a')[1];
if(ary[splt]==true) {
this.firstChild.nodeValue='hide '+(parseFloat(splt)+1);
ary[splt]=false;
showdiv(splt);
return false;
}
else {
this.firstChild.nodeValue='show '+(parseFloat(splt)+1);
ary[splt]=true;
document.getElementById('d'+splt).className='hide';
return false;
}
}
}
}
}
function showdiv(num) {
for(var c=0;c<divs.length;c++){
document.getElementById('a'+c).firstChild.nodeValue='show '+(c+1);
document.getElementById('a'+num).firstChild.nodeValue='hide '+(parseFloat(num)+1);
ary[c]=true;
ary[num]=false;
}
for(c=0;c<divs.length;c++) {
if(divs[c].className=='show') {
divs[c].className='hide';
}
}
document.getElementById('d'+num).className='show';//<--- this is the line error
}

function Init() {
init();
showdiv(0);
}


if(window.addEventListener){
window.addEventListener('load',Init,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',Init);
}
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum