Solved

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

Posted on 2016-07-29
3
43 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now