Solved

Trying to addClass to a menu item based off the title of the page using jquery.

Posted on 2016-07-29
3
62 Views
Last Modified: 2016-07-29
Hi,
hope this makes some sense....
I have a menu and the items are set dynamically using a loop and I bring in the sidebar containing the menu with an include.
So each of the <li> in the menu have the id of the menu name which is also the page title.
I'm trying to set the class to 'active' for whichever menu item we are on.

For example
The menu item corresponding to my "Used Inventory" page is:
<li id="Used Inventory">Used Inventory</li> (which is also the page title tag)

So below I'm trying to figure out how to set a class of 'active' to that <li> based off the title tag.
Obviously I'm failing :(
Any idea how to get this done?

<script>
    	$('#' + $(document).attr('title')).addClass('active');
</script>

Open in new window

0
Comment
Question by:tjyoung
[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
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 41735359
Without seeing the full page structure, I'd be willing to guess, that your script is at the top of the page?
Nothing wrong with that, but in that case the rest of the page hasn't actually loaded yet when you call your script, so the target element isn't in the dom.

Wrap the function above in $(document).ready() and that might fix.

<script>
	$(document).ready(function(){
	    	$('#' + $(document).attr('title')).addClass('active');
	});
</script>

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
ID: 41735361
Thanks. No difference. I'm guessing my reference to the id tag is probably wrong?
I was guessing how to format that.
<script>
$( document ).ready(function() {
    	$('#' + $(document).attr('title')).addClass('active');
    	});
</script>

Open in new window

Could it be it doesn't have any quotes around the #Used Inventory instead of '#Used Inventory'
That being the case, not sure how to get those extra 2 quotes in there
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 41735367
Its late... I must be getting sleepy.
That was the fix.
Thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Title # Comments Views Activity
Date on a table 16 35
converting document.write() to innerHTML 7 48
Increase counter and attr inside a while loop 15 36
replacing inline javascript with jquery 4 45
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to dynamically set the form action using jQuery.
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…

749 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