Solved

javascript syntax

Posted on 2012-04-08
6
214 Views
Last Modified: 2012-04-09
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
Comment
Question by:rhyno99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 2

Assisted Solution

by:Artic
Artic earned 100 total points
ID: 37821263
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
 

Author Comment

by:rhyno99
ID: 37821299
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
 
LVL 23

Accepted Solution

by:
basicinstinct earned 350 total points
ID: 37821786
the syntax is this:

document.getElementById("menu2").style.backgroundColor = "#F00";
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 50 total points
ID: 37822585
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
 
LVL 2

Assisted Solution

by:Artic
Artic earned 100 total points
ID: 37822916
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
 

Author Closing Comment

by:rhyno99
ID: 37826338
Thank you all
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question