...

View Full Version : Showing and Hiding Divs within document.write()



halian
02-15-2007, 08:11 PM
Hey all,

I've been working on a small internal "message board" and i've run into a snag. I've decided to put the subject and text into an array and run a loop to display them all. Here's that part of the code I've included the other functions just in case, but the important one is switcheroo():

function switcheroo(id){
var el = document.getElementById(id);
if(el.style.display == "none"){
el.style.display = "block";
}else{
el.style.display = "none";
}
}

function cursor_hand(){
document.body.style.cursor = 'hand';
}
function cursor_norm(){
document.body.style.cursor = 'default';
}

function message(subject,text){
this.subject = subject;
this.text = text;
}

messageBoard = new Array();
messageBoard[0] = new message("Hey there. I'm a subject","Hello there. I'm the body text of this here message. Love it or leave it nubsauce.");
messageBoard[1] = new message("Hey there. I'm a subject too","Hello there. I'm the body text of the second message. Deal with it.")
the problem comes when trying to show / hide the divs with document.write()
here's the document.write loop:

for(i=0;i<=messageBoard.length;i++){
var menuID = "menu"+i;
var subID = "sub"+i;
document.write("<span id='"+menuID+"' onmouseover='cursor_hand()' onclick='switcheroo('"+subID+"')' onmouseout='cursor_norm()'>"+messageBoard[i].subject+"</span><br /><div id='"+subID+"' style='display:none'><hr size='1' />"+messageBoard[i].text+"<hr size='1' /></div>");
}

If you take the text between the ()'s on the document.write line and manually put that in the body...everything works fine. So that leads me to believe that the function itself is correct.

If you leave it as is, the loop will list each subject and (if you change the display to "block") each text to go with it. But the onclick doesn't hide / show the div as expected. I'm not receiving any errors from the browser.

Any ideas?

MikeFoster
02-15-2007, 08:30 PM
Hi halian, Welcome to Coding Forums!

My changes are in red:


document.write("<span id='" + menuID + "' onmouseover='cursor_hand()' onclick=\"switcheroo('"+subID+"')\" onmouseout='cursor_norm()'>"+messageBoard[i].subject+"</span><br /><div id='"+subID+"' style='display:none'><hr size='1' />"+messageBoard[i].text+"<hr size='1' /></div>");

halian
02-15-2007, 08:49 PM
Thanks alot....something SO simple...hard to believe i missed it...but with all the 's and "s i suppose it could happen to anyone. Thanks for the fresh eyes.

-Halian



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum