Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Showing and Hiding Divs within document.write()

    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():
    Code:
    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:
    Code:
    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?

  • #2
    Regular Coder
    Join Date
    May 2004
    Location
    Alabama, USA
    Posts
    237
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi halian, Welcome to Coding Forums!

    My changes are in red:
    Code:
    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>");

  • #3
    New to the CF scene
    Join Date
    Feb 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

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