Jump to content

[SOLVED] Changing div background colors by function


Darkmatter5

Recommended Posts

Here's my code:

<script type="text/javascript">
function toggleDiv(divid,menu) {
    document.getElementById("default").style.display="none";
    document.getElementById("sitesettings").style.display="none";
    if(document.getElementById(divid).style.display=="none") {
        document.getElementById(menu).style.backgroundColor="#CC9999";
        document.getElementById(divid).style.display="block";
    }
    else {
        document.getElementById(menu).style.backgroundColor="#CCCC99";
        document.getElementById(divid).style.display="none";
    }
}
</script>

<div id="menu_default" style="font-weight: bold; padding: .2em 1em; background-color: #CCCC99;">
  <a href="javascript:;" onclick="toggleDiv('default',this)">Admin default page</a>
</div>
<div id="menu_sitesettings" style="font-weight: bold; padding: .2em 1em; background-color: #CCCC99;">
  <a href="javascript:;" onclick="toggleDiv('sitesettings',this)">Site settings</a>
</div>
<div id="default" style="display: block;">TEST1</div>
<div id="sitesettings" style="display: none;">TEST2</div>

 

If the code is run like this the second line of the if and else statements breaks and the info divs won't display.  If I comment out the

"document.getElementById(menu).style.backgroundColor="#CC9999";" or "document.getElementById(menu).style.backgroundColor="#CCCC99";"

lines which are lines 1 of the if else statements the divs display and hide correctly, but the menu divs backgroudn colors don't change.  What's wrong?

Link to comment
Share on other sites

Okay the script is now:

 

function toggleDiv(divid,menu) {
    document.getElementById("default").style.display="none";
    document.getElementById("sitesettings").style.display="none";
    document.getElementById("menu_default").style.backgroundColor="#CCCC99";
    document.getElementById("menu_sitesettings").style.backgroundColor="#CCCC99";
    if(document.getElementById(divid).style.display=="none") {
        menu.style.backgroundColor="#CC9999";
        document.getElementById(divid).style.display="block";
    }
    else {
        document.getElementById(divid).style.display="none";
    }
}

 

[*]When the div background color is changed it only changes the color around the text, not the entire div.  Why not?

[*]If you click another link it doesn't change the originally div color back.  It just keeps chaning the newly clicked link to the new color and never back to the "off" position.

Link to comment
Share on other sites

parentNode worked great thanks!

 

In regards to my other question.  Say you click menu_default and the background color changes, then you click menu_sitesettings.  I want to change menu_default back to #CCCC99 and change the newly clicked div (menu_sitesettings) to #CC9999.  Basically it's toggling the divs colors as you click them.

Link to comment
Share on other sites

This code works:

 

function toggleDiv(divid,menu) {
    var divs=new Array();
    var menudivs=new Array();
    divs[0]=document.getElementById("default");
    divs[1]=document.getElementById("sitesettings");
    divs[2]=document.getElementById("newsmanagement");
    divs[3]=document.getElementById("miscellaneous");
    divs[4]=document.getElementById("usermanagement");
    menudivs[0]=document.getElementById("menu_default");
    menudivs[1]=document.getElementById("menu_sitesettings");
    menudivs[2]=document.getElementById("menu_newsmanagement");
    menudivs[3]=document.getElementById("menu_miscellaneous");
    menudivs[4]=document.getElementById("menu_usermanagement");
    var i=0;
    for (i=0;i<=4;i++) {
        divs[i].style.display="none";
        menudivs[i].style.backgroundColor="#CCCC99";
    }
    if(document.getElementById(divid).style.display=="none") {
        menu.parentNode.style.backgroundColor="#CC9999";
        document.getElementById(divid).style.display="block";
    }
    else {
        document.getElementById(divid).style.display="none";
    }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.