Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

javascript syntax

Posted on 2012-04-08
6
Medium Priority
?
217 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 400 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 1400 total points
ID: 37821786
the syntax is this:

document.getElementById("menu2").style.backgroundColor = "#F00";
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 200 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 400 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

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!

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

670 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