Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

javascript syntax

Hello Experts,
I am trying to make a two part menu in this script a different section of the menu shows a different div in a different color depending on what page the user is on. I am having a bit of trouble getting the syntax right. Can you take a look?
Thanks,
rhyno99

window.onload=function(){
document.getElementById("menu2").style.display="none"
      if (window.location.toString().indexOf("index") > -1) {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
      else if (window.location.toString().indexOf("K-2") > -1) {
      document.getElementById("catk-2").style.display="block";
      document.getElementById("menu2").css background-color:'#F90';
      }
      else if (window.location.toString().indexOf("3-5") > -1) {
      document.getElementById("cat3-5").style.display="block";
      document.getElementById("menu2").css background-color:'#06F';
      }
      else if (window.location.toString().indexOf("6-8") > -1) {
      document.getElementById("cat6-8").style.display="block";
      document.getElementById("menu2").css background-color:'#0F0';
      }
      else {
      else if (window.location.toString().indexOf("9-12") > -1) {
      document.getElementById("cat9-12").style.display="block";
      document.getElementById("menu2").css background-color:'#FF0';
      }
      
      else {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
}
0
rhyno99
Asked:
rhyno99
4 Solutions
 
ArticCommented:
You had two else statements. Try this:

window.onload=function(){
document.getElementById("menu2").style.display="none"
      if (window.location.toString().indexOf("index") > -1) {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
      else if (window.location.toString().indexOf("K-2") > -1) {
      document.getElementById("catk-2").style.display="block";
      document.getElementById("menu2").css background-color:'#F90';
      }
      else if (window.location.toString().indexOf("3-5") > -1) {
      document.getElementById("cat3-5").style.display="block";
      document.getElementById("menu2").css background-color:'#06F';
      }
      else if (window.location.toString().indexOf("6-8") > -1) {
      document.getElementById("cat6-8").style.display="block";
      document.getElementById("menu2").css background-color:'#0F0';
      }
      else if (window.location.toString().indexOf("9-12") > -1) {
      document.getElementById("cat9-12").style.display="block";
      document.getElementById("menu2").css background-color:'#FF0';
      }
      else {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
}
0
 
rhyno99Author Commented:
Thanks, whoops that was in copying. Here is what I have but it still doesn't work.

window.onload=function(){
document.getElementById("menu2").style.display="none"
      if (window.location.toString().indexOf("index") > -1) {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
      else if (window.location.toString().indexOf("K-2") > -1) {
      document.getElementById("catk-2").style.display="block";
      document.getElementById("menu2").css background-color:'#F90';
      }
      else if (window.location.toString().indexOf("3-5") > -1) {
      document.getElementById("cat3-5").style.display="block";
      document.getElementById("menu2").css background-color:'#06F';
      }
      else if (window.location.toString().indexOf("6-8") > -1) {
      document.getElementById("cat6-8").style.display="block";
      document.getElementById("menu2").css background-color:'#0F0';
      }
      else if (window.location.toString().indexOf("9-12") > -1) {
      document.getElementById("cat9-12").style.display="block";
      document.getElementById("menu2").css background-color:'#FF0';
      }
      else {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").css background-color:'#F00';
      }
}
0
 
basicinstinctCommented:
the syntax is this:

document.getElementById("menu2").style.backgroundColor = "#F00";
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ProculopsisCommented:
Try using a switch to make your code more readable:

switch (document.location.href.replace(/.*(index|k-2|3-5|6-8|9-12).*/, "$1")) {
    case "index":
        // code here
        break;

    case "k-2":
        // code here
        break;

    case "3-5":
        // code here
        break;

    case "6-8":
        // code here
        break;

    case "9-12":
        // code here
        break;

    default:
        // code here
        break;
}

Open in new window

0
 
ArticCommented:
Needs to be style.backGroundColor not .css background-color. Try this:

window.onload=function(){
document.getElementById("menu2").style.display="none";
      if (window.location.toString().indexOf("index") > -1) {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#F00";
      }
      else if (window.location.toString().indexOf("K-2") > -1) {
      document.getElementById("catk-2").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#F90";
      }
      else if (window.location.toString().indexOf("3-5") > -1) {
      document.getElementById("cat3-5").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#06F";
      }
      else if (window.location.toString().indexOf("6-8") > -1) {
      document.getElementById("cat6-8").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#0F0";
      }
      else if (window.location.toString().indexOf("9-12") > -1) {
      document.getElementById("cat9-12").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#FF0";
      }
      else {
      document.getElementById("welcome").style.display="block";
      document.getElementById("menu2").style.backgroundColor = "#F00";
      }
};
0
 
rhyno99Author Commented:
Thank you all
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now