Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

919 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

21 Experts available now in Live!

Get 1:1 Help Now