Solved

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

Posted on 2016-07-29
3
54 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
  • 2
3 Comments
 
LVL 22

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
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…

777 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