Solved

javascript syntax

Posted on 2012-04-08
6
213 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
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
Industry Leaders: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

685 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