htrain26 Posted January 8, 2015 Share Posted January 8, 2015 Hello, (please accuse me for not using technical terms correctly and such, I am a newbie) I'm trying to add an "active" class to two buttons in a nav menu...one goes to the main/top section button and the other on the btn in the drop down. The code that I'm using works, but I have to do this for three pages and when I add the others some buggy stuff starts to happen. Here's the code that I'm using (at least for the home page and the "public safety" section of the site): <script> var current = window.location.href; if ( current == "http://www.tfmcomm.com" || current == "http://www.tfmcomm.com/" || current == "http://www.tfmcomm.com/index.htm" ){ jQuery('#mainmenu > li > a[href=/index.htm]').parent().addClass("current-menu-item"); } else if ( current == "http://www.tfmcomm.com/motorola/accessories/public-safety/" ){ jQuery('.sub-menu > li > a[href$=/motorola/accessories/public-safety/]').parent().addClass("current-menu-item"), jQuery('#mainmenu > li > a[href$=/public-safety/index.htm]').parent().addClass("current-menu-item"); } else if ( current.indexOf("http://www.tfmcomm.com/public-safety/") > -1 || current.indexOf("http://www.tfmcomm.com/motorola/accessories/public-safety") > -1 ){ jQuery('#mainmenu > li > a[href$=/public-safety/index.htm]').parent().addClass("current-menu-item"); }</script> The code in red is where I seem to be running into a problem. As you can see I'm just separating the two .addClass lines with a "comma". I have a feeling that's not legit. I tried to find the correct way to go about this, but with no luck. Any and all help would be greatly appreciated. Thanks in advance, m@ Link to comment Share on other sites More sharing options...
Ingolme Posted January 8, 2015 Share Posted January 8, 2015 You don't need a comma, end the lines with a semi-colon, the comma might be interpretted as a syntax error, though I haven't tested to make sure. You can put as many instructions inside an if block as you want and you can call addClass() as often as you want. If the class isn't being added then your selector probably isn't pointing to the right element. Link to comment Share on other sites More sharing options...
htrain26 Posted January 8, 2015 Author Share Posted January 8, 2015 I had tried that along with not using anything and few other trial and error goofs. When I would have either the semi-colon, comma or nothing between the two they would work independently. But when I would add one or both of the other instances, things would get wonky. I changed it back to your suggested semi-colon's and everything seems to be working fine as of now. I must have overlooked something. Either way, thank you!!! I've been looking at this too long. Thanks again! Link to comment Share on other sites More sharing options...
dsonesuk Posted January 9, 2015 Share Posted January 9, 2015 Maybe you were thinking of this?jQuery('.sub-menu > li > a[href$=/motorola/accessories/public-safety/]','#mainmenu > li > a[href$=/public-safety/index.htm]').parent().addClass("current-menu-item");Should work Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now